Skip to content

Instantly share code, notes, and snippets.

@kejiweixun
Last active July 22, 2018 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kejiweixun/44e26ac939c99c69801c4a7726227698 to your computer and use it in GitHub Desktop.
Save kejiweixun/44e26ac939c99c69801c4a7726227698 to your computer and use it in GitHub Desktop.
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