Comments (6)
So it actually seems like the .gate() step is completing all of it's steps successfully. However, it isn't bundling the messages from the completion triggers together into an array like the example in the README suggests. The arguments object looks like this:
{ '0': { [Function: a] fail: [Function], abort: [Function] },
'1':
{ /* config object */ },
'2':
{ /* config object */ },
'3':
{ /* config object */ }
}
from asynquence.
Your functionality is mostly correct, but just a slight misunderstanding of how the arguments will arrive after a gate. The message(s) from each gate-segment will respectively take up one positional argument to the next sequence step's callback. If a gate-segment sends one message, then it will just be one argument. If a gate-segment sends 2 or more messages, then in that positional argument, there will be an array of the messages that gate-segment sent. Example:
ASQ()
.gate(
function(done) { done("1","2"); },
function(done) { done("3"); },
function(done) { done("4","5"); }
})
.val(function(){ // <-- Notice: `val` instead of `then`
var messages = Array.prototype.slice.call(arguments);
console.log(JSON.stringify(messages); // [ ["1","2"], "3", ["4","5"] ]
});
A few notes:
-
I used
val()
instead ofthen()
because I don't need thedone
parameter, and everything in that step will execute synchronously (just outputting). If I had usedthen()
, the behavior would have been the same except thedone
callback would have been on the beginning of thearguments
array. We'd have wanted to ditch it by callingmessages.shift()
, for example. I saved that mess by usingval()
which skips thedone
callback and only passes in messages.If, at the end of the
val()
call, you want to pass on another message to the next step in the chain, you can justreturn
that message, and it will implicitly be passed along. You can only pass along one message this way. -
A positional argument is only an array if that corresponding gate-segment sent more than one message. Otherwise, the message itself would be in that positional argument.
-
If a gate-segment sends no message,
undefined
will be in that corresponding positional argument.
from asynquence.
Ah I see. I should have noticed that. Thanks for the writeup.
from asynquence.
BTW, here's some suggested "improvements" to the organization to take advantage of some of asynquence API functionality.
function getEndpointConfig(endpoint) {
return ASQ()
.then(function(done){
endpoint.getConfig(function(err,config){
if (err) {
done.fail(err);
}
else {
config.id = endpoint.id;
console.log(config.id);
done(config);
}
});
})
;
}
// ASQ-chain
.seq(function(endpoints) { // for this example, endpoints is an array of two object.
var calls = [];
endpoints.forEach(function(endpoint) {
calls
.push(function(done) {
getEndpointConfig(endpoint).pipe(done); // <-- Notice: `pipe`
});
});
return ASQ()
.gate.apply(this,calls)
;
})
.val(function() { // <-- Notice: `val` instead of `then`
var configs = Array.prototype.slice.call(arguments);
console.log(configs);
var configMap = _.pluck(configs, 'id');
console.log(configMap);
})
.or(function(err){
console.log(err);
});
Some notes:
- I use
done.fail(...)
to signal an error into the chain, and then at the bottom of the chain, I "catch" any of those with theor(..)
handler. - I use
val()
here again, same reasons as given before - I pushed the implementation of the config-fetching into its own
getEndpointConfig()
function, and then had it return a sequence, then I usedpipe(done)
to wire up both the message and error channels from that sequence back into the main sequence.
from asynquence.
Also, I just updated the README to try to clarify the way gate messages (in and out) work... can you see if this helps makes it clearer?
from asynquence.
Looks good! Thanks for the clarification. I took your advice on that refactor as well, much cleaner.
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
- Combining Generators with Promises Using Asynquence HOT 2
- 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.