【Promise()】promise
The constructor is primarily used to wrap functions that do not already support promises.異步
executoride
A function that is passed with the arguments resolve
and reject
. The executor
function is executed immediately by the Promise implementation, passing resolve
and reject
functions (the executor is called before the Promise
constructor even returns the created object). url
紅字部分:executor在new Promise()返回前就被執行。spa
A Promise
is in one of these states:code
Promise.all(iterable)
blog
Returns a promise that either fulfills when all of the promises in the iterable argument have fulfilled or rejects as soon as one of the promises in the iterable argument rejects. If the returned promise fulfills, it is fulfilled with an array of the values from the fulfilled promises in the same order as defined in the iterable. If the returned promise rejects, it is rejected with the reason from the first promise in the iterable that rejected. This method can be useful for aggregating results of multiple promises.ip
所有異步promise結束,或其中一個出錯。get
爲了讓一個異步調用有 Promise能力,返回一個new Promise()便可。it
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(); }); };
參考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise