Comments (10)
I'm not sure what you mean by "wrong" order? You call done()
which tells the sequence that on the next available tick of JS execution, it's OK to move on. But then you do some more work by doing a setTimeout(). Are you expecting the setTimeout() itself to somehow delay the sequence from moving on?
from asynquence.
The setTimeout is there just to emulate a slower async execution (in my case was an ajax call).
What I would expect is for each sequence step to be allowed to run only if the "done()" inside the previous sequence step is called, not by the call of "any" done(). So in my example I would expect the third item (printing 3) to wait for the done() call in the second item (printing 2). I would prefer that each done callback passed to each sequence step would be strictly bound to trigger that specific step conclusion, not as a global sequence trigger. In this way the expected order (1, 2, 3, 4) would be always respected, as it always happens if you remove the accidental second call to done() in the first step.
from asynquence.
The done is bound to only that step, and to only be executed once (subsequent calls of the same done are ignored). In your code, the first call to done marks the sequence as ok to advance to the second step, sets up the 10 timeout, advances to the next step, sets up the 500 timeout, then prints 1.1 from the first timeout, then prints 2 from the second timeout, then calls the second step's done, which advances the sequence to step 3, and so on. This is all reflected by the output as you show.
This is all entirely by design. I'm not sure where your confusion might be, but your code above does exactly what it should do.
Perhaps you're missing that step 1 tries to call its done both before and after the 10 timeout? Perhaps your confusion is that setTimeout somehow hooks into the inner workings of the sequence to control it... but setTimeout is a JS thing entirely independent of the sequence.
Can you try to clarify why you think the done is not step-bound, because it absolutely is?
from asynquence.
I think they were confused because apparently the output was
1
1.1
3
4
2
instead of
1
1.1
2
3
4
from asynquence.
Nope, this is not what is happening, both 3 and 4 are printed before 2 in my example. I added some messages so it is clear who is triggering what:
asq()
.then(function(done) {
console.log(1);
done('first step trigger');
setTimeout(function() {
// second call by mistake
done('first step trigger (too)');
}, 10);
})
.then(function(done, msg) {
setTimeout(function() {
console.log(2, 'triggered by ', msg);
done('second step trigger');
}, 500);
})
.then(function(done, msg) {
console.log(3, 'triggered by ', msg);
done('third step trigger');
})
.then(function(done, msg) {
console.log(4, 'triggered by ', msg);
done();
})
;
And this is the output I'm getting:
1
3 "triggered by " "first step trigger (too)"
4 "triggered by " "third step trigger"
2 "triggered by " "first step trigger"
As you can see, both 3 and 2 are triggered by done calls inside the first step, so the done function is not actually bound to a single step and the subsequent call is not ignored.
The 3 is coming before the 2 because it is triggered by the first step before the 2 is finished.
from asynquence.
This is a pen reproducing what I said (please open the console to see the output)
http://codepen.io/SkidX/pen/XNqQZr
from asynquence.
@SkidX, I was saying that you got 3
and 4
before 2
instead of the expected one. I saw the pen, and that does look like a bug. If you comment out the second done()
it works as expected.
from asynquence.
@legodude17, sorry, when I said "nope..." I was answering to @getify but I forgot to mention him.
Yes, as you said removing the second done() it works exactly as expected, but this means that subsequent calls to the same done function are not actually ignored.
from asynquence.
Sorry I missed the problem in ordering of 3
and 4
in the OP. Shouldn't have been trying to triage a bug while on a mobile device. :)
Verified bug. I had tests in the test suite that were supposed to catch this, but didn't. :(
Fix incoming.
from asynquence.
Thanks a lot for the quick fix, I just tested it also in my real world scenario and it works perfectly.
from asynquence.
Related Issues (20)
- `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.