Answer the question
In order to leave comments, you need to log in
Array Promise exception handling?
Good afternoon.
There is an array of promises, you need to execute them in parallel (asynchronously). How can I do that?
let arr = [new Promise[pending],new Promise[pending],new Promise[pending]
One solution I found was Promise.All([arr]). But some promises are fulfilled with reject, and Promise.All works if promises resolve.
For example I have an array Promise of
const cmd = require('node-cmd-promise');
let promisesArray = []
let array = ['google.ru', 'yandex.ru', 'habrahabr.ru', 'yana.ru']
for (let i = 0;i < array.length; ++i) {
promisesArray.push(cmd(`ping ${array[i]}`));
}
console.log(promisesArray)
Promise.all(promisesArray).catch((error) => console.log(error)).then(results => console.log(results))
Answer the question
In order to leave comments, you need to log in
Did something similar recently. Something like this:
const cmd = require('node-cmd-promise');
const array = ['google.ru', 'yandex.ru', 'habrahabr.ru', 'yana.ru']
const promisesArray = array.map((url) => {
return cmd(`ping ${array[i]}`)
.then((pingResult) => Promise.resolve(pingResult))
.catch(error => {
console.error(error);
return Promise.resolve(null);
})
});
Promise.all(promisesArray).then((result) => {
const finished = result.filter(result => result !== null);
console.log('succesfully pinged: ', finished);
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question