0

There is a MDN spec Async function,refer function async function parallel() which I am trying to implement as part of a larger project for parallel processing ,but All my promises are not being generated or being resolved .Only one promise of the PromiseAll has gone "fulfilled" state(as seen in debugger).I am new student to JS and Promises.

console.log("==PARALLEL with await Promise.all==");
        parallel();
    // Definition of parallel function to Start 5 "jobs" in parallel and wait for both of them to complete 
        async function parallel() {
        await Promise.all([
        (async () => await a()),
        (async () => await b()),
        (async () => await c()),
        (async () => await d()),
        (async () => await e()),
        ]);
        }
        function a()
        {
        console.log("In function a ");
        return Promise.resolve(1);
        }
        function b(){
        console.log("In function b ");
        return Promise.resolve(2);
        }
        function c(){
        console.log("In function c ");
        return Promise.resolve(3);
        }
        function d(){
        console.log("In function d ");
        return Promise.resolve(4);
        }
        function e(){
        console.log("In function e ");
        return Promise.resolve(5);
        }
<div>Test Promises</div>
Michael36
  • 150
  • 11
  • I'd say, lose the IIFEs, they only obscure your code. `await Promise.all([a(), b(), c(), d(), e()]);` will give you the same result. – Emiel Zuurbier Oct 08 '22 at 08:53
  • Coming back to a query...Right IIFE obscure the code ,but why is it compulsory else the functions never run?it seems a bit awkward – Michael36 Oct 08 '22 at 09:02

2 Answers2

0

You are not calling the functions. See the example below. It was difficult to explain what 'you are not calling' means, so once you see the working example let me know and we will delete this question

console.log("==PARALLEL with await Promise.all==");
        parallel();
    // Definition of parallel function to Start 5 "jobs" in parallel and wait for both of them to complete 
        async function parallel() {
        await Promise.all([
        (async () => await a())(),
        (async () => await b())(),
        (async () => await c())(),
        (async () => await d())(),
        (async () => await e())(),
        ]).then(console.log);
        }
        function a()
        {
        console.log("In function a ");
        return Promise.resolve(1);
        }
        function b(){
        console.log("In function b ");
        return Promise.resolve(2);
        }
        function c(){
        console.log("In function c ");
        return Promise.resolve(3);
        }
        function d(){
        console.log("In function d ");
        return Promise.resolve(4);
        }
        function e(){
        console.log("In function e ");
        return Promise.resolve(5);
        }
<div>Test Promises</div>
ibrahim tanyalcin
  • 5,643
  • 3
  • 16
  • 22
0

console.log("==PARALLEL with await Promise.all==");
// Code to Start 5 "jobs" in parallel and wait for both of them to complete
parallel();
async function parallel() {

await Promise.all([
(async () => await a())(),
(async () => await b())(),
(async () => await c())(),
(async () => await d())(),
(async () => await e())(),
]);
}
function a()
{
console.log("In function a ");
return Promise.resolve(1);
}
function b(){
console.log("In function b ");
return Promise.resolve(2);
}
function c(){
console.log("In function c ");
return Promise.resolve(3);
}
function d(){
console.log("In function d ");
return Promise.resolve(4);
}
function e(){
console.log("In function e ");
return Promise.resolve(5);
}
Michael36
  • 150
  • 11