等待异步功能,直到返回数据

时间:2020-10-13 19:27:58

标签: javascript node.js deno

我有此代码:

let getUserPosts = async (id: number) =>{
const access =  new DatabaseAccess();
await access.init();
let videosArray: any = [];
const subscriptions = await access.getSubscriberTargetIds(id);
// @ts-ignore
 await subscriptions.forEach(async function(thing: any){
    const posts = await access.getUserPosts(thing.target_id)
    videosArray = [...videosArray, ...posts]
    console.log(videosArray)//console logging here works fine
});
 console.log(videosArray)//here i have an empty array
while(videosArray == []){
    console.log('waitin till that')
}
return videosArray
}

我需要将其返回到主要的get函数,以向用户显示供稿作为响应。 该代码正在Deno上运行,但我确定它适用于Node。

0 个答案:

没有答案