Whats the smartest / cleanest way to iterate async over arrays (or objs)?

后端 未结 4 1263
礼貌的吻别
礼貌的吻别 2020-12-01 04:45

Thats how I do it:

function processArray(array, index, callback) {
    processItem(array[index], function(){
        if(++index === array.length) {
                  


        
4条回答
  •  旧时难觅i
    2020-12-01 05:01

    The easiest way to handle async iteration of arrays (or any other iterable) is with the await operator (only in async functions) and for of loop.

    (async function() {
     for(let value of [ 0, 1 ]) {
      value += await(Promise.resolve(1))
      console.log(value)
     }
    })()

    You can use a library to convert any functions you may need which accept callback to return promises.

提交回复
热议问题