I have an object that has promises as its values. The object fields could have different types of values such as functions, promises, strings, numbers, or another object. How do I implement a function which waits for all the promises in the object to resolve?
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"
// }
// }
})