This file contains hidden or 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
const p1 = Promise.resolve(42); | |
const p2 = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(52); | |
}, 100); | |
}); | |
Promise.all([p1, p2]) | |
.then(msgs => { | |
console.log(msgs[0]); //42 |
This file contains hidden or 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
new Promise(resolve => { | |
resolve(1); | |
Promise.resolve().then(() => console.log(2)); | |
console.log(3); | |
}).then(t => console.log(t)); | |
console.log(4); |
This file contains hidden or 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
const p = new Promise((resolve, reject) => { | |
resolve(21); | |
}) | |
p | |
.then(v => { | |
console.log(v); // 21 | |
return new Promise((resolve, reject) => { | |
resolve(v * 2); | |
}); |
This file contains hidden or 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
function foo(x) { | |
return new Promise((resolve, reject) => { | |
//進行非同步任務 | |
//成功就呼叫resolve() | |
//失敗就呼叫reject() | |
}); | |
} | |
var p = foo(42); |
This file contains hidden or 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
foo(x) { | |
//進行非同步任務 | |
return listener; | |
} | |
var evt = foo(42); | |
evt.on('completion', () => { | |
//do something | |
}); |
This file contains hidden or 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
analytics.trackPurchase(purchaseData, ()=> { | |
chargeCreditCard(); | |
displayThankyouPage(); | |
}); |
This file contains hidden or 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
analytics.trackPurchase(purchaseData, ()=> { | |
chargeCreditCard(); | |
displayThankyouPage(); | |
}); |
This file contains hidden or 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
analytics.trackPurchase(purchaseData, ()=> { | |
chargeCreditCard(); | |
displayThankyouPage(); | |
}); |
This file contains hidden or 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
analytics.trackPurchase(purchaseData, ()=> { | |
chargeCreditCard(); | |
displayThankyouPage(); | |
}); |
This file contains hidden or 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
analytics.trackPurchase(purchaseData, ()=> { | |
chargeCreditCard(); | |
displayThankyouPage(); | |
}); |