S
S
sheeel2019-07-12 16:59:57
JavaScript
sheeel, 2019-07-12 16:59:57

Async / await arrays?

How to properly write an asynchronous array map with an asynchronous function call

//gid = [ 3417744,  3413239,  4054641,  3297954,  4818643,  4610528,  3994962 ]
let results = await gid.map((id)=> parseID(id)) // не работает
let results = await gid.map((id)=> await parseID(id)) // ошибка

Help

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2019-07-12
@sheeel

const results = await Promise.all(gid.map(id => parseID(id)));

S
SagePtr, 2019-07-12
@SagePtr

Through Promise.all, and in the map function return a promise that does something with the current element

let results = await Promise.all(gid.map(async id => await parseID(id)));

D
davidnum95, 2019-07-12
@davidnum95

As far as I remember, there is no way to sequentially through the map, only through the usual cycles, for...in for example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question