gpt4 book ai didi

javascript - 如何嵌套 Promise(Promise.all 内的 Promise.all)

转载 作者:行者123 更新时间:2023-11-28 03:13:51 26 4
gpt4 key购买 nike

考虑以下嵌套的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)
});

由于某种原因,我没有到达代码的末尾(End 没有被打印)。我怀疑有一些位置或缺少返回,但我陷入困境而没有找到解决方案。

如何使此代码结构按预期工作,如下所示:

  1. 遍历源(我的数据来自数据库
  2. 对于每个寄存器,应用引用更改(在示例中更改年龄)
  3. 返回固定引用的数据

此代码的预期结果是使用当前的 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"
}
]

最佳答案

给你!

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);
});

关于javascript - 如何嵌套 Promise(Promise.all 内的 Promise.all),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59830323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com