我有一个类似数组的结构,它公开了异步方法。异步方法调用返回数组结构,该结构又公开更多的异步方法。我正在创建另一个JSON对象来存储从这个结构中获得的值,因此我需要小心跟踪回调中的引用。
我已经编写了一个暴力解决方案,但我想学习一个更惯用或更干净的解决方案。
以下是一些部分代码-
var jsonItems = [];
items.forEach(function(item){
var jsonItem = {};
jsonItem.name = item.name;
item.getThings().then(function(things){
// or Promise.all(allItemGetThingCalls, function(things){
things.forEach(function(thing, index){
jsonItems[index].thingName = thing.name;
if(thing.type === 'file'){
thing.getFile().then(function(file){ //or promise.all?
jsonItems[index].filesize = file.getSize();
https://stackoverflow.com/questions/31413749
复制相似问题