Friday 2 June 2017

Using a function on all elements of a list

Basically what I'm trying to do is, say I have a function, something() that returns a promise and a whole lot of values that I would like to resolve

let values = ['123', '456', '789']

How can I resolve all of them while avoiding doing something like this

let temp = [];
let newValue;

for(let x = 0; x < values.length; x++) {
    newValue = await something(values[x]);
    temp.push(newValue);
}

Reason is because I don't want to have to await for the promises to be resolved before moving onto the next loop. Is there any way I can just await all at once? and have them be in a list? Or would the method be no different than awaiting them all in order using the for loop.



via CyberLX

No comments:

Post a Comment