Lodash: is it possible to use map with async functions?

后端 未结 4 1220
孤街浪徒
孤街浪徒 2021-02-12 13:10

Consider this code

const response  = await fetch(\'\');
const responseJson = await response.json();
responseJson =  _.sortBy(responseJson, \"number         


        
4条回答
  •  轮回少年
    2021-02-12 13:32

    To process your response jsons in parallel you may use Promise.all:

    const responseJson = await response.json();
    responseJson = _.sortBy(responseJson, "number");
    
    let result = await Promise.all(_.map(responseJson, async (json) => 
      await addEnabledProperty(json))
    );
    

    Since addEnabledProperty method is async, the following also should work (per @CRice):

    let result = await Promise.all(_.map(responseJson, addEnabledProperty));
    

提交回复
热议问题