I m trying to write await for multiple promises, instead of nested functions.
Please take a look at the below code i tried, as it will explain better than me.
var main = async () => {
// const main_ = await Promise.all(fun1,fun2,fun3);
// Fun 3
const fun3 = () =>
new Promise((resolve) => async () => {
// console.log(1);
return resolve(await fun2(1));
});
// Fun 2
const fun2 = (value) =>
new Promise((resolve) => async (value) => {
value = value + 1;
// console.log(value);
return resolve(await fun1(value));
});
// Fun 1
const fun1 = () =>
new Promise((resolve) => (value) => {
value = value + 1;
// console.log(value);
return resolve(value);
});
fun3();
};
main();
I tried console logging to debut but I m getting nothing in the console.
Any help is greatly appreciated.
4
Answers
I hope this example works for you.
the syntax is wrong, its not
new Promise((resolve) => async () => {})
with 2 arrow, butnew Promise((resolve) => {})
also you can call promise function withoutawait
fun3() is an async function so you have to put
await
before the call.If you want to call
await
inside a Promise callback, you can do this:So with this in mind, you can rewrite your functions like this:
If you want to do it in pure async/await, you may do this: