Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
I write these codes to help me understand js promise
let result = 9;
function a(){
console.log(-1)
return new Promise(function(resolve, reject){
console.log(0)
resolve('a');
console.log(1)
});
};
function b(message){
return new Promise(function(resolve, reject){
console.log(2)
resolve('b' + message);
});
};
function c(message){
return new Promise(function(resolve, reject){
console.log(3)
resolve('c' + message);
});
};
a().then(function(result){
console.log(4)
return b(result);
}).then(function(resultFromB){
console.log(5)
return c(resultFromB);
}).then(function(resultFromC){
console.log(6);
[...result] = resultFromC;
console.log(result);
});
console.log(result);
// -1
//0
//1
//9
//4
//2
//5
//3
//6
//[ 'c', 'b', 'a' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment