简体   繁体   English

子功能和异步等待

[英]Child functions and async await

In one of my API endpoints I fetch a json resource (1) from the web and edit it to fit my needs. 在我的一个API端点中,我从Web获取json资源(1)并对其进行编辑以满足我的需要。 In the "lowest" or "deepest" part of the tree I'm trying to fetch another resource and add it to the final json object. 在树的“最低”或“最深”部分,我正在尝试获取另一个资源并将其添加到最终的json对象中。 I'm relatively new to async/await but am trying to move away from the "old" Promise s since I see the advantage (or the gain) of using async/await. 我对async / await相对较新,但我试图摆脱“旧”的Promise因为我看到了使用async / await的优势(或收益)。

The object from (1) looks like; 来自(1)的对象看起来像;

 const json = { date, time, trips: [{ name, legs: [{ id }, { id } ] }] }; 

Here's how I "reformat" and change the json object; 这是我如何“重新格式化”并更改json对象;

 { date, time, trips: json.trips.map(trip => formatTrip(trip)) }; function formatTrip(trip) { return { name, legs: trip.legs.map(leg => formatLeg(leg)) }; }; async function formatLeg(leg) { const data = await fetch(); return { id, data }; }; 

The problem with this is that after I've "reformatted/edited" the original json to look how I want it (and ran through all format... functions) the legs objects are empty {} . 这个问题是,在我“重新格式化/编辑”原始json以查看我想要它(并遍历所有format...函数)之后, legs对象是空的{}

I figured this might be due to the async/await promises not finishing. 我想这可能是因为async / await promises没有完成。 I've also read that if a child-function uses async/await all the higher functions has to use async/await as well. 我还读过,如果子函数使用async / await,所有更高级的函数也必须使用async / await。

Why? 为什么? How can I rewrite my code to work and look good? 如何重写我的代码才能工作并且看起来很好? Thanks! 谢谢!

EDIT: 编辑:

I updated my code according to Randy's answer. 我根据Randy的回答更新了我的代码。 getLegStops(leg) is still undefined/empty. getLegStops(leg)仍未定义/为空。

 function formatLeg(leg) { return { other, stops: getLegStops(leg) }; }; function getLegStops(leg) { Promise.all(getLegStopRequests(leg)).then(([r1, r2]) => { /* do stuff here */ return [ /* with data */ ]; }); }; function getLegStopRequests(leg) { return [ url1, url2 ].map(async url => await axios.request({ url })); }; 

Two things lead you to want to nest these Promises: 有两件事让你想要嵌套这些Promises:

  1. The old way of thinking about callbacks and then Promises 关于回调然后承诺的旧方法
  2. Believing the software process must match the data structure 相信软件过程必须与数据结构相匹配

It appears you only need to deal with the Promises once if I understand correctly. 如果我理解正确,你似乎只需要处理一次Promises。

Like this: 像这样:

 async function getLegs(){ return trip.legs.map(async leg => await fetch(...)); // produces an array of Promises } const legs = Promise.all(getLegs()); function formatLegs(legs) { // do something with array of legs }; function formatTrip(){ //format final output } 

EDIT: per your comment below, this snippet represents what I've demonstrated and what your goal should look like. 编辑:根据下面的评论,此代码段代表我演示的内容以及您的目标应该是什么样子。 Please review your code carefully. 请仔细检查您的代码。

 const arr = [1, 2, 3, ]; const arrPromises = arr.map(async v => await new Promise((res) => res(v))); const finalPromise = Promise.all(arrPromises); console.log(finalPromise.then(console.log)); 

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

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