Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sean1093 committed Oct 6, 2018
1 parent dcff4be commit c819d1b
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions js/javascript-advance-learning-note.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 產生後,使用 <code>then</code> 來分別指定 <code>resolve</code> 和 <code>reject</code> 的 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)
Expand Down

0 comments on commit c819d1b

Please sign in to comment.