JavaScript Promise()
The Promise() constructor creates Promise objects.
A Promise represents the eventual completion or failure of an asynchronous operation.
Promise objects are commonly used with asynchronous functions and APIs.
Symtax
new Promise(executor)
Parameters
| Parameter | Description |
|---|---|
| executor | A function that receives resolve and reject functions. |
Return Value
| Call | Returns |
|---|---|
new Promise(...) |
A Promise object |
Creating a Promise
Example
const myPromise = new Promise(function(resolve, reject) {
resolve("Success");
});
Promise States
A Promise can be in one of three states:
- pending
- fulfilled
- rejected
| State | Description |
|---|---|
| pending | Initial state |
| fulfilled | Operation completed successfully |
| rejected | Operation failed |
Consuming a Promise
Example
myPromise.then(function(value) {
console.log(value);
});
Promise Methods
then()catch()finally()
Static Promise Methods
Promise.resolve()Promise.reject()Promise.all()Promise.allSettled()Promise.any()Promise.race()
Promises should usually be consumed with then(), catch(), or await.
Object Hieraky
Promise objects inherit from Object.
Object └─ Promise
| Constructor Type | Function |
| Creates | Promise objects |
| Inherits From | Object |
Related Pages
Browser Support
new Promise() is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
| Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
| May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |