A Taste of JavaScript Promise
JavaScript Promises are used to track the state of an asynchronous task.
|
|
Promise states
- fulfilled - the action relating to the promise successed
- rejected - the action relating to the promise failed
- pending - hasn’t fulfilled or rejected yet
- settled - has fulfilled or rejected
A promise can be
resolved to
either a promise or thenable, in which case it will store the promise or thenable for later unwrapping.
how to create a promise
|
|
how to use promise
|
|
then()
takes two arguments, callback for a success case, and another for the failure case. Both are optional, so you are able to add a callback for the success or failure case only.
Resources:
Guide to JavaScript Promise, Promise API Reference
Update (2017/06/11)
The purpose of async/await function is to simplify the behavior of using promises synchronously and to perform some behavior on a group of
Promises
. Just likePromises
are similar to structured callbacks,async/await
is similar to combining generators and promises. -MDN
Also read Why Async/Await is better than Promise