Consider the following structure of nested 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). 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:
age
)The expected result of this code is to get the original data ( source
) with the available references fixed, using the current promise structures:
[
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); });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.