Skip to content

Instantly share code, notes, and snippets.

@alvinsj
Created June 27, 2016 12:37
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 alvinsj/e4efc00b868c7396a42f2b3e57d16346 to your computer and use it in GitHub Desktop.
Save alvinsj/e4efc00b868c7396a42f2b3e57d16346 to your computer and use it in GitHub Desktop.
var p1, p2, tp1;
// p1 is original promise
p1 = new Promise(function(resolve, reject){
setTimeout(function(){
console.log('+ resolve to hello');
resolve("hello");
}, 1000)
});
// p2 is a new promise after p1.then()
p2 = p1.then(function(value){
console.log("expect p1.then() got 'hello':", value == "hello" || value);
console.log("expect p1 and p2 are not the same instance:", p1 !== p2);
// tp1 is internal promise, used for Promise.resolve
tp1 = Promise.resolve("yep");
console.log('+ resolve to yep, with tp1');
return tp1;
});
p2.then(function(value){
console.log("expect p2.then() got 'yep':", value == "yep" || value);
console.log("expect p1 and p2 and tp1 are not the same instance:", !(p1 === p2 && tp1 === p2));
console.log('+ reject with error');
return Promise.reject("error");
}).catch(function(value){
console.log("expect p2.catch got 'error':", value === 'error' || value);
});
p1.catch(function(value){
console.log("this should not be shown");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment