Promise.allSettled & Promise.all

Promise.allSettled & Promise.all

Promise

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Promise.allSettled & Promise.all_第1张图片

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled

Promise.allSettled

demo

const log = console.log;

const promise1 = Promise.resolve(1);
// const promise2 = new Promise((resolve, reject) => setTimeout(reject, 0, '❌ Error'));
// const promise3 = new Promise((resolve, reject) =>  setTimeout(resolve, 0, '✅ OK'));

const promise2 = Promise.reject('❌ Error');
const promise3 =Promise.resolve('✅ OK');

const promises = [promise1, promise2, promise3];

Promise.allSettled(promises). then(
  (results) => results.forEach(result => log(`result`, result.status, result))
);


/*

result fulfilled {status: "fulfilled", value: 1}
result rejected {status: "rejected", reason: "❌ Error"}
result fulfilled {status: "fulfilled", value: "✅ OK"}

Promise {: undefined}

*/


Promise.allSettled & Promise.all_第2张图片


Promise.all

const promiseFunction = async () => {
  const resolvingPromise = new Promise(resolve => setTimeout(() => resolve('success'), 1000));
  const rejectingPromise = new Promise((resolve, reject) => setTimeout(() => reject('fail'), 500));
  try {
    const allPromises = await Promise.all([resolvingPromise, rejectingPromise]);
  } catch (e) {
  	// e is 'fail', which is  the contents of the reject function from rejectingPromise
    // resolvingPromise =  Promise {}
    console.log(e);
  }
};
promiseFunction();

Promise.allSettled

const promiseFunction = async () => {
  const resolvingPromise = new Promise(resolve => setTimeout(() => resolve('success'), 1000));
  const rejectingPromise = new Promise((resolve, reject) => setTimeout(() => reject('fail'), 500));
  try {
    const allPromises = await Promise.allSettled([resolvingPromise, rejectingPromise]);
    console.log(allPromises);
    // allPromises
    // [
    //   {status: "fulfilled", value: 'success'},
    //   {status: "rejected",  reason: 'fail'}
    // ]
  } catch (e) {
    // this code block is never executed
    console.log(e);
  }
};
promiseFunction();

Promise.race

const promiseFunction = async () => {
  const resolvingPromise = new Promise(resolve => setTimeout(() => resolve('success'), 1000));
  const resolvingPromiseTwo = new Promise((resolve, reject) => setTimeout(() => resolve('successTwo'), 500));
  try {
    const racedPromises = await Promise.race([resolvingPromise, resolvingPromiseTwo]);
    console.log(racedPromises);
    // both promises would've resolved, but resolvingPromiseTwo was faster, so racedPromises = 'successTwo'`
  } catch (e) {
    // this code block is only executed if the first promise to settle rejects/throws
    console.log(e);
  }
};
promiseFunction();

refs

https://www.yuque.com/sunluyong/interview/ars1bd

https://blog.jonlu.ca/posts/promises


Flag Counter

©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


你可能感兴趣的:(Promise.allSettled & Promise.all)