Promise.race() 多个已解决的承诺

Promise.race() multiple resolved promises

The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.

取自 MDN 站点。

我有 5 个承诺,我需要知道一旦 任何 2 个承诺得到解决,同时考虑性能。

const sleep = ms =>
  new Promise(r => setTimeout(r, ms))

async function swimmer (name) {
  const start = Date.now()
  console.log(`${name} started the race`)
  await sleep(Math.random() * 5000)
  console.log(`${name} finished the race`)
  return { name, delta: Date.now() - start }
}

const swimmers =
  [ swimmer("Alice"), swimmer("Bob"), swimmer("Claire"), swimmer("David"), swimmer("Ed") ];

Promise.race(swimmers)
  .then(({ name }) => console.log(`*** ${name} is the winner!!! ***`))
  .catch(console.error)

这将是 return 最快的游泳者,但我想在我解决 2 个承诺后打印出来。

我该怎么做?

您可以编写 Promise.race 的自定义实现,returns 一个 promise 的结果是 2 个先于其他 promise 解决的 promise。

以下代码示例显示了一个实现:

function customPromiseRace(promiseArr, expectedCount) {
   return new Promise((resolve, reject) => {
      if (promiseArr.length < expectedCount) {
        throw new Error(`Not enough promises to get ${expectedCount} results`);
      }
      // array to store the results of fulfilled promises
      const results = [];  

      for (const p of promiseArr) {
        Promise.resolve(p).then(result => {
          // push the promise fulfillment value to the "results"
          // array only if we aren't already finished
          if (results.length < expectedCount) {
            results.push(result);
          
            if (results.length === expectedCount) {
              resolve(results);
            }
          }
        }, reject);
      }
   });
}

演示

const sleep = ms => new Promise(r => setTimeout(r, ms));

async function swimmer(name) {
  const start = Date.now();
  console.log(`${name} started the race`);
  await sleep(Math.random() * 5000);
  console.log(`${name} finished the race`);
  return { name, delta: Date.now() - start };
}

const swimmers = [
  swimmer('Alice'),
  swimmer('Bob'),
  swimmer('Claire'),
  swimmer('David'),
  swimmer('Ed'),
];

function customPromiseRace(promiseArr, expectedCount) {
   return new Promise((resolve, reject) => {
      if (promiseArr.length < expectedCount) {
        throw new Error(`Not enough promises to get ${expectedCount} results`);
      }
      const results = [];  

      for (const p of promiseArr) {
        Promise.resolve(p).then(result => {
          if (results.length < expectedCount) {
            results.push(result);
            if (results.length === expectedCount) {
              resolve(results);
            }
          }
        }, reject);
      }
   });
}

customPromiseRace(swimmers, 2).then(console.log).catch(console.error);

您可以在 Promise.race 中使用 .then(handleMyPromise)

示例:

let handlepromise1 = function ( response ){
    console.log("Expected response: ",response);
    return response;
}

let promise1 = function () {
    return new Promise((r) => {
        setTimeout(()=>{
            console.log("Promise1 says");
            r("promise1")
        },2000)
    })
}

let timeout = function(){
    return new Promise((r) => {
        setTimeout(()=>{
            console.log("timeout says");
            r("timeout")
        },3000)
    });
};

(async function(){
    let func = async function tt(){
        let r = await Promise.race([
            promise1().then(handlepromise1),
            timeout()
        ])

        console.log("Result: ",r);
    }

    func()
})()

控制台结果:

Promise1 says
Expected response:  promise1
Result:  promise1
timeout says