diff --git a/js/javascript-advance-learning-note.md b/js/javascript-advance-learning-note.md index 85e88da..94edca8 100644 --- a/js/javascript-advance-learning-note.md +++ b/js/javascript-advance-learning-note.md @@ -37,27 +37,54 @@ See more: https://www.impressivewebs.com/callback-functions-javascript/ 從 ES6 之後才出現的,主要是可以避免層層 callback 造成所謂的 (callback hell)。 -basic promise example: +Basic promise example: 建立一個 Promise Object ```js -const promise = new Promise(function(resolve, reject) { +const promise = new Promise((resolve, reject) => { if (true) { resolve("ok!"); } else { - reject(Error("It broke")); + reject(Error("Fail")); } }); +``` + +Promise object 產生後,使用 then 來分別指定 resolvereject 的 callback function -promise.then(function(result) { + +```js +promise.then((result) => { console.log(result); // "ok!" -}, function(err) { +}, (err) => { console.log(err); // Error: "It broke" }); ``` -reference: https://eyesofkids.gitbooks.io/javascript-start-es6-promise/content/ +Promise 建立後會立即執行 + +```js +const promise = new Promise((resolve, reject) => { + console.log('Promise Create'); + resolve(); +}); + +promise.then(() => { + console.log('Hello World'); +}); + +console.log('End'); + +// Promise Create +// End +// Hello World +``` + +### reference: + +* https://eyesofkids.gitbooks.io/javascript-start-es6-promise/content/ +* http://es6.ruanyifeng.com/#docs/promise ## Arrow Function (ES6)