You-dont-know-js: Is this sentence correct? Chapter 3 "Calling Too Late"

Created on 28 Jan 2018  路  2Comments  路  Source: getify/You-Dont-Know-JS

I found a sentence in Chapter 3 "Calling Too Late" that made me scratch my head.
(right below the code snippet)

p.then( function(){
    p.then( function(){
        console.log( "C" );
    } );
    console.log( "A" );
} );
p.then( function(){
    console.log( "B" );
} );
// A B C

_Here, "C" cannot interrupt and precede "B", by virtue of how Promises are defined to operate._

It looks to me, that it is possible that "C" can precede "B". If "B" takes longer to resolve than "A + C" the results are being logged in the following order. // A C B
So by design, promises do not prevent "C" from preceding "B".
https://jsfiddle.net/datadeer/7u0nk7fp/3/

Am i on to something, or am i missing anything?

Most helpful comment

If the then(..) calls were on different promises, what you say would be possible. But in the example quoted, all then(..) calls are on the same p promise. As such, once that one p is resolved, all these then(..) registered handlers will run in a single first-come-first-served queue, which would then ensure A B C order.

All 2 comments

If the then(..) calls were on different promises, what you say would be possible. But in the example quoted, all then(..) calls are on the same p promise. As such, once that one p is resolved, all these then(..) registered handlers will run in a single first-come-first-served queue, which would then ensure A B C order.

That makes sense. I haven't thought about that. Thanks for the quick response. :)

Was this page helpful?
0 / 5 - 0 ratings