我有一个对象,其值为promise。对象字段可以具有不同类型的值,例如函数,promise,字符串,数字或其他对象。 如何实现一个等待对象中所有promise解析的函数?
const asyncFunction = () => {
return Promise.resolve("Resolved!")
}
const nonAsyncFunction = () => {
return "Resolved!"
}
const awaitObjectWithPromise = (obj) => {
// Returns a promise which waits for all
// the promises inside "obj" to be resolved.
}
let obj = {
key1: asyncFunction(),
key2: nonAsyncFunction(),
key3: "Some Value",
parent1: {
key1: asyncFunction(),
key2: nonAsyncFunction(),
key3: "Some Value"
}
}
awaitObjectWithPromise(obj).then((obj) => {
console.log(obj);
// Should output:
// {
// key1: "Resolved!",
// key2: "Resolved!",
// key3: "Some Value",
// parent1: {
// key1: "Resolved!",
// key2: "Resolved!",
// key3: "Some Value"
// }
// }
})
答案 0 :(得分:3)
您可以遍历对象的键并兑现承诺。
const asyncFunction = () => {
return Promise.resolve("Resolved!")
}
const nonAsyncFunction = () => {
return "Resolved!"
}
const awaitObjectWithPromise = async(obj) => {
for (let prop in obj) {
// If the propriety has a 'then' function it's a Promise
if (typeof obj[prop].then === 'function') {
obj[prop] = await obj[prop];
}
if (typeof obj[prop] === 'object') {
obj[prop] = await awaitObjectWithPromise(obj[prop]);
}
}
return obj;
}
let obj = {
key1: asyncFunction(),
key2: nonAsyncFunction(),
key3: "Some Value",
parent1: {
key1: asyncFunction(),
key2: nonAsyncFunction(),
key3: "Some Value"
}
}
awaitObjectWithPromise(obj).then((obj) => {
console.log(obj);
});
答案 1 :(得分:2)
在接受的答案中,我对某些事情不满意:
null
或 undefined
,则会引发错误;为了解决这些问题,我改变了函数:
const promiseAll = async (obj) => {
if (obj && typeof obj.then == 'function') obj = await obj;
if (!obj || typeof obj != 'object') return obj;
const forWaiting = [];
Object.keys(obj).forEach(k => {
if (obj[k] && typeof obj[k].then == 'function') forWaiting.push(obj[k].then(res => obj[k] = res));
if (obj[k] && typeof obj[k] == 'object') forWaiting.push(promiseAll(obj[k]))
});
await Promise.all(forWaiting);
return obj;
}
也许对某人有用。