V
V
vasyok2282021-10-14 22:32:28
Node.js
vasyok228, 2021-10-14 22:32:28

How to abort Promise.all?

Hello. Tell me how you can interrupt promise.all, I will give an example below

let promisess = this.distribution.files.map(async(res) => {

// код......

})
Promise.all(promisess).then(() => {
    // все отлично, без ошибок

}).catch(err => {
// Есть ошибка, останавливаем обработку promisess
})


I’ve been googling and googling since 10 am, I haven’t found anything accessible and working. I would be very grateful if anyone can advise

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Aetae, 2021-10-15
@vasyok228

Something like this:

function makeRejectable(...promisess) {
  let reject;
  const rejector = new Promise((_, r) => reject = r);
  
  const promise = Promise.race([
    Promise.all(promisess),
    rejector
  ]);
  
  return [promise, reject]
}

const [promise, reject] = makeRejectable(...promisess);
  
promise.then(console.log, console.error);
  
reject('reject');

S
Sergey, 2021-10-14
@KingstonKMS

Here _

let promisess = this.distribution.files.map(async(res) => {
    return new Promise((resolve, reject) => {
    // код
    // если код выполнился с ошибкой - reject(), без ошибок - resolve()
    });
});

Promise.all(promisess).then(() => {
    // все отлично, без ошибок

}).catch(err => {
// Есть ошибка, останавливаем обработку promisess
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question