简体   繁体   English

如何使用循环获得 promise 的结果?

[英]How to a result of a promise using loop?

I need to manipulate the result of a promise inside a loop, but the return is empty.我需要在循环内操作 promise 的结果,但返回为空。

When I remove AWAIT the return comes with promise.当我删除 AWAIT 时,返回的是 promise。 But this behavior is not what I need.但这种行为不是我需要的。

I need this function to return the final data to me.我需要这个 function 将最终数据返回给我。

Could anyone tell me how I can solve this problem?谁能告诉我如何解决这个问题?

I thank the attention我感谢关注

I'm using lodash to loop and AdonisJs for the query query我正在使用 lodash 循环和 AdonisJs 进行查询

    const result = _.transform(cods, async (rr: any, vv: any, ii: any) => {
         const items = await Estoque.query().preload('porto').preload('armaz').whereIn('siaId', vv).orderBy('validade', 'asc')
         const itemsSerelialize = items.map((item) => item.serialize())
    
         rr.push(itemsSerelialize)
    
    }, [])

console.log(result) //empty

This is because lodash is working synchronously, it doesn't wait until your await funfills.这是因为lodash是同步工作的,它不会等到您的await funfills 完成。 I have found a library to make lodash usable with async functions, you can try this out Aigle for lodash我找到了一个库,可以让lodashasync函数一起使用,你可以试试Aigle for lodash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM