How to use Array.prototype.filter with async?

前端 未结 5 1827
闹比i
闹比i 2020-12-29 01:56

Background

I am trying to filter an array of objects. Before I filter, I need to convert them to some format, and this operation is asynchronous.

         


        
5条回答
  •  悲哀的现实
    2020-12-29 02:14

    There is no way to use filter with an async function (at least that I know of). The simplest way that you have to use filter with a collection of promises is to use Promise.all and then apply the function to your collection of results. It would look something like this:

    const results = await Promise.all(your_promises)
    const filtered_results = results.filter(res => //do your filtering here)
    

    Hope it helps.

提交回复
热议问题