Promise constructor is primarily used to wrap functions that do not already support promises.

The source for this interactive demo is stored in a GitHub repository. If you'd like to contribute to the interactive demo project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

句法

new Promise(executor)
					

参数

executor
function to be executed by the constructor, during the process of constructing the promiseObj .  The executor is custom code that ties an outcome to a promise. You, the programmer, write the executor .  The signature of this function is expected to be:
function(resolutionFunc, rejectionFunc){
    // typically, some asynchronous operation.
}
							
At the time when the constructor generates the new promiseObj , it also generates a corresponding pair of functions for resolutionFunc and rejectionFunc; these are "tethered" to the promiseObj .  Therefore, the code within the executor has the opportunity to perform some operation and then reflect the operation's outcome(If the value is not another Promise object) as either "fulfilled" or "rejected" by terminating with an invocation of either the resolutionFunc rejectionFunc , respectively.
executor has no meaningful return value. It communicates via the side-effect caused by resolutionFunc or rejectionFunc . The side-effect is that the promiseObj becomes "resolved."
Typically, it works like this:  The operation within executor is asynchronous and provides a callback. The callback is defined within the executor code. The callback terminates by invoking resolutionFunc .  The invocation of resolutionFunc includes a value parameter. The value is passed back to the tethered promiseObj .  The promiseObj (asynchronously) invokes any .then() associated with it. The value received by promiseObj.then() is passed to the invocation of handleFulfilled as an input parameter (See "Chained Promises" section).
executor might also include a try{} catch() block that invokes rejectionFunc upon error.
The signatures of these two functions are simple, they accept a single parameter of any type. Of course, the actual names of these functions can be whatever is desired, i.e. they are named as the parameters of executor .  Each function is used by simply calling it when appropriate.
resolutionFunc(value) // call on fulfilled
rejectionFunc(reason) // call on rejected
							

返回的 value can be another promise object, in which case the promise gets dynamically inserted into the chain.

返回值

promiseObj
When called via new Promise constructor returns a promise object. The promise object will become "resolved" when either of the functions resolutionFunc or rejectionFunc are invoked. Note that if you call resolutionFunc or rejectionFunc and pass another Promise object as an argument, you can say that it is "resolved", but still cannot be said to be "settled".

范例

Creating a new Promise

A Promise object is created using the new keyword and its constructor. This constructor takes a function, called the "executor function", as its parameter. This function should take two functions as parameters. The first of these functions ( resolve ) is called when the asynchronous task completes successfully and returns the results of the task as a value. The second ( reject ) is called when the task fails, and returns the reason for failure, which is typically an error object.

const myFirstPromise = new Promise((resolve, reject) => {
  // do something asynchronous which eventually calls either:
  //
  //   resolve(someValue)        // fulfilled
  // or
  //   reject("failure reason")  // rejected
});
							

Making functions return a Promise

To provide a function with promise functionality, have it return a promise:

function myAsyncFunction(url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest()
    xhr.open("GET", url)
    xhr.onload = () => resolve(xhr.responseText)
    xhr.onerror = () => reject(xhr.statusText)
    xhr.send()
  });
}
							

规范

规范
ECMAScript (ECMA-262)
The definition of 'Promise constructor' in that specification.

浏览器兼容性

To contribute to this compatibility data, please write a pull request against this repository: https://github.com/mdn/browser-compat-data . 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Promise() 构造函数 Chrome 32 Edge 12 Firefox 29
29
Constructor requires a new operator since version 37.
IE No Opera 19 Safari 8
8
Constructor requires a new operator since version 10.
WebView Android 4.4.3 Chrome Android 32 Firefox Android 29
29
Constructor requires a new operator since version 37.
Opera Android 19 Safari iOS 8
8
Constructor requires a new operator since version 10.
Samsung Internet Android 2.0 nodejs 0.12
0.12
Constructor requires a new operator since version 4.

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: