Code:

var x = new Promise((resolve, reject) => { setTimeout( function() { console.log( 'x done' ); resolve() }, 1000 ); }); Promise.resolve().then(x).then((resolve, reject) => { console.log( 'all done' ); }); 

Output:

all done x done 

Expected output:

x done all done 

Why is the promise x not waiting to resolve before calling the next then callback?

JSFiddle:

8

1 Answer

So as you want to run promises in a series you should convert x to function and call it in then:

function x() { return new Promise(resolve => { setTimeout(() => { console.log('x done'); resolve() }, 1000); }); }); Promise.resolve() .then(x) .then(() => console.log('all done')); 

or simplest variant:

x().then(() => console.log('all done')); 

jsfiddle demo

12

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy