Comments (2)
Thank you Kyle for taking the time to respond! It means a lot, and I got it! :)
from asynquence.
Do you see this part in your first snippet?
function(err, res) {
G.next(res);
}
What's happening here is that a function callback is in the style of "error-first callback" -- that's what fs.readdir(..)
is expecting to receive for its callback -- meaning the first param is reserved for the error signal. In this form, you're basically ignoring/throwing away any error that might come through, and assuming success.
What'd you'd probably want to do is:
function(err, res) {
if (err) G.throw(err);
else G.next(res);
}
Here we're splitting the behavior on error vs success, and passing along the appropriate signal (message or exception) to the generator code to handle. This is more robust.
Anyway, either in your version of the snippet, or in mine, you'll notice that the signature maps from 2 inputs (err,res
) down to one input (err
or res
). That's what you're missing in your second snippet.
You pass along the done
callback to fs.readdir(..)
, so of course it's going to receive both err
and res
, and that's why you're seeing the null
in the first argument.
To fix this, you have two options.
Option 1:
function readDirectory(dir) {
return ASQ(function(done){
fs.readdir(dir,function(err,...success){
if (err) done.fail(err);
else done(...success);
});
});
}
See how we're now splitting the behavior and either passing along only the error, or only whatever success messages come through?
But, instead of doing this manually, asynquence provides a convenience helper that does this automatically.
Option 2:
function readDirectory(dir) {
return ASQ(function(done){
fs.readdir(dir,done.errfcb);
});
}
Notice done.errfcb
. There's no (..)
because we're not calling that function... we're passing along the error-first version of the done(..)
callback for fs.readdir(..)
to call. done.errfcb
is automatically created for you to use in cases where you need an error-first style callback form. :)
Actually, there's an alternate form for option 2 if you prefer.
Option 2b:
function readDirectory(dir) {
var sq = ASQ();
fs.readdir( dir, sq.errfcb() );
return sq;
}
The difference here is that we're constructing the sequence instance (sq
), then actually calling errfcb()
on the instance to produce the error-first style callback as a sequence step in sq
, then passing that callback into fs.readdir(..)
. Finally, we return that sq
sequence so someone else can wait on that step to complete (or error)`.
A separate issue is also that you should be "listening" for any of those signaled exceptions. You can either do that on the sequence instance directly:
ASQ().runner(genProm)
.or(function(err){ .. });
Or, more preferably, you can handle it inside the generator, with a try..catch
:
function *genProm() {
try {
let exercises = yield readDirectory(dirname);
console.log(exercises);
}
catch(err) {
// ..
}
}
Hope that's helpful info for you!
from asynquence.
Related Issues (20)
- Sequence's order of execution may be wrong in some cases HOT 10
- `runner(..)` plugin bug w/ multiple generators
- ARRAY_SLICE is not defined. HOT 4
- pass all message in map()
- improve `wrap(..)`
- Thanks for asynquence HOT 1
- async map HOT 4
- JSBin examples not working HOT 3
- How to extend an ASQ iterable sequence while it's running? HOT 4
- Babel 6: missing ) after argument list (asq.src.js line 66) HOT 6
- loop over an array in series? HOT 6
- Continue onerror fail HOT 2
- [question] why do u use setImmediate not process.nextTick ? HOT 4
- contrib: fix wrapped generators and arguments
- test-extensions are in contrib for some reason HOT 3
- [README]: Enlarge build section to be about contrib (Maybe), add 'Contributing' section HOT 4
- autod error
- ASQ.iterable() is not a function... HOT 5
- ES6 asynquence-contrib importing issue HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asynquence.