H
H
Herberito Galustyan2019-09-30 20:41:18
JavaScript
Herberito Galustyan, 2019-09-30 20:41:18

How to write a Promise.race function in reverse?

How to write the Promise.race function in reverse?. It must return the last of the promises. Without using Promise.race(), Promise.all(), Promise.allSettled().

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-09-30
@Herberto

const last = promises => new Promise((resolve, reject) => {
  let pending = promises.length;

  if (!pending) {
    resolve();
  } else {
    promises.forEach(n => n
      .then(result => --pending || resolve(result))
      .catch(error => --pending || reject(error))
    );
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question