Last active
July 22, 2018 08:54
-
-
Save kejiweixun/44e26ac939c99c69801c4a7726227698 to your computer and use it in GitHub Desktop.
I write these codes to help me understand js promise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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