简体   繁体   English

如何嵌套承诺(Promise.all 内的 Promise.all)

[英]How to nest promises (Promise.all inside Promise.all)

Consider the following structure of nested Promises :考虑以下嵌套Promises结构:

 const getData = async() => { const refs = [{ name: "John33", age: 33 }, { name: "John34", age: 34 }, { name: "John35", age: 35 }, { name: "John36", age: 36 } ]; let source = [{ name: "John30", age: "unknown" }, { name: "John31", age: "unknown" }, { name: "John32", age: "unknown" }, { name: "John33", age: "unknown" }, { name: "John34", age: "unknown" }, { name: "John35", age: "unknown" }, { name: "John36", age: "unknown" }, { name: "John37", age: "unknown" }, { name: "John38", age: "unknown" }, { name: "John39", age: "unknown" } ]; const resolver = doc => { return new Promise(doc => { let clone = { ...doc }; let found = refs.find(ref => { return ref.name === doc.name; }); if (found) clone.age = found.age; return clone; }); }; let getRefs = (doc, refs) => { const promises = refs.map(r => { resolver(doc).then(result => { return result; }); }); return Promise.all(promises); }; let getCursorData = (cursor, refs, data) => { const promises = cursor.forEach(doc => { console.log("Getting cursor for " + doc.name); let clone = { ...doc }; return getRefs(clone, refs).then(result => { console.log("Getting refs for " + clone.name); data.push(result); }); return; }); return Promise.all(promises); }; // Get data let data = []; await getCursorData(source, refs, data); console.log("Returned data: "); console.log(data); return data; }; console.log("Begin"); getData().then(result => { console.log("End"); console.log(result) });

For some reason I'm not getting to the end of the code ( End is not being printed).出于某种原因,我没有到代码的末尾( End没有被打印)。 I suspect there is some position or missing return, but I'm stuck without finding the solution.我怀疑有一些位置或缺少回报,但我在没有找到解决方案的情况下被卡住了。

How can I make this code structure works as expected, as follows:我怎样才能使这个代码结构按预期工作,如下所示:

  1. Iterate through source (my data that comes from database遍历源(我的数据来自数据库
  2. For each register, apply reference changes (in the example change the age )对于每个寄存器,应用引用更改(在示例中更改age
  3. Return the data with the references fixed返回固定引用的数据

The expected result of this code is to get the original data ( source ) with the available references fixed, using the current promise structures:此代码的预期结果是使用当前的 promise 结构获取具有固定可用引用的原始数据( source ):

[
      name: "John30",
      age: "unknown"
    },
    {
      name: "John31",
      age: "unknown"
    },
    {
      name: "John32",
      age: "unknown"
    },
    {
      name: "John33",
      age: 33
    },
    {
      name: "John34",
      age: 34
    },
    {
      name: "John35",
      age: 35
    }, {
      name: "John36",
      age: 36
    },
    {
      name: "John37",
      age: "unknown"
    },
    {
      name: "John38",
      age: "unknown"
    },
    {
      name: "John39",
      age: "unknown"
    }
]

here you go!干得好!

 const SimulatedDatabaseCall = new Promise(resolve => { setTimeout(() => { resolve([ { name: 'John33', age: 33 }, { name: 'John34', age: 34 }, { name: 'John35', age: 35 }, { name: 'John36', age: 36 } ]); }, 200); }); let source = [ { name: 'John30', age: 'unknown' }, { name: 'John31', age: 'unknown' }, { name: 'John32', age: 'unknown' }, { name: 'John33', age: 'unknown' }, { name: 'John34', age: 'unknown' }, { name: 'John35', age: 'unknown' }, { name: 'John36', age: 'unknown' }, { name: 'John37', age: 'unknown' }, { name: 'John38', age: 'unknown' }, { name: 'John39', age: 'unknown' } ]; async function updateSource(source, SimulatedDatabaseCall) { await SimulatedDatabaseCall; SimulatedDatabaseCall.then(_database => { var sourceMap = source.map(_sourceOBJ => { return _sourceOBJ.name; }); _database.forEach(_databaseOBJ => { var index = sourceMap.indexOf(_databaseOBJ.name); source[index].age = _databaseOBJ.age; }); }); return source; } updateSource(source, SimulatedDatabaseCall).then(_val => { console.log(_val); });

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

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