Promises in the loop

30.12.19

Consider an example of performing promises in a loop. In this task, the execution order was not important.

let promises = [];

removedList.forEach(param => {
  promises.push(
    this.apiMethod(param.uid).toPromise().then(res => {
     
      list.splice(list.findIndex(el => el.id == param.id), 1);

    })
  )
});

Promise.all(promises).then(() =>{
  //do something...
  console.log('success');
}).catch(e => {
  console.log(e);
}).finally(() => {
  
  removedList = [];
  console.log('done');
});
blog comments powered by Disqus