Node async / wait with a promise. all

In a simple JS program, I need asyncOperation2 and asyncOperation3 to be executed sequentially along with asyncOperation1. Meaning I need the following:

1) order of execution as  1,2,3 or 2,3,1
2) then after step 1. I need to preform an operation on the result of 1,2,3

Here is what I have so far (this will work in Node). But I cannot get the order of operations as described above.

const App = {
    total: 0,

    init: function () {
        // I want to run all 3 operations. (with 2 and 3 happening sequentially)
        Promise.all([App.asyncOperation1(1000), App.combine2and3()])
            .then((total) => {
                // then I want to use the result of all 3 operations
                console.log(`total from top: ${total}`);
            })
    },

    asyncOperation1: function (time) {
        return new Promise(function (resolve, reject) {
            var intervalID = setTimeout(function () {
                resolve(time);
            }, time);
        });
    },

    asyncOperation2: async function (time) {
        setTimeout(function () {
            return time;
        }, time);
    },

    asyncOperation3: async function (time) {
        setTimeout(function () {
            return time;
        }, time);
    },

    combine2and3: async function () {
        var value2 = await App.asyncOperation2(2000);
        var value3 = await App.asyncOperation3(1000);
        console.log(`value2: ${value2}`)
        console.log(`value3: ${value3}`)
        console.log(`summed total ${value2 + value3}`);
        return value2 + value3;
    },
};

App.init();

Actual Results:

value2: undefined
value3: undefined
summed total NaN
total from top: 1000,NaN

Desired Results:

value2: 2000
value3: 1000
summed total 3000
total from top: 1000,3000
+4
source share
3 answers

The problem is that asyncOperation2(), and asyncOperation3()return the promise (because of their ads async), which has not allowed value (so it undefined), since these functions do not have a return value.

asyncOperation2: async function (time) {
    setTimeout(function () {
        return time;     // this just returns back into the timer sub-system
    }, time);
    // there is no return value for your function here
    // thus the promise the async function returns has an undefined resolved value
},

setTimeout() . , , . , setTimeout() . - , , ( , setTimeout() ). , setTimeout() , , , - - .

:

function delay(t, v) {
   return new Promise(resolve => {
       setTimeout(resolve.bind(null, v));
   }, t);
}

asyncOperation2: function (time) {
    return delay(time, time);
},

asyncOperation3: function (time) {
    return delay(time, time);
},

, , . : async, await , .

+2

asyncOperation2 asyncOperation3 , - . ( combine2and2).

. setTimeout .

Promise , .

-

const App = {
    total: 0,

    init: function () {
        // I want to run all 3 operations. (with 2 and 3 happening sequentially)
        Promise.all([App.asyncOperation1(1000), App.combine2and3()])
            .then((total) => {
                // then I want to use the result of all 3 operations
                console.log(`total from top: ${total}`);
            })
    },

    asyncOperation1: function (time) {
        return new Promise(function (resolve, reject) {
            var intervalID = setTimeout(function () {
                resolve(time);
            }, time);
        });
    },

    asyncOperation2:  function (time) {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve(time);
        }, time);
        });
    },

    asyncOperation3:  function (time) {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            resolve(time);
        }, time);
        })
    },

    combine2and3: async function () {
        var value2 = await App.asyncOperation2(2000);
        var value3 = await App.asyncOperation3(1000);
        console.log(`value2: ${value2}`)
        console.log(`value3: ${value3}`)
        console.log(`summed total ${value2 + value3}`);
        return value2 + value3;
    },
};

App.init();
Hide result
+2

return time , setTimeout, , . setTimeout , , asyncOperation1. , .

const wait = time => new Promise(
  resolve => setTimeout(() => resolve(time), time)
);

const App = {
    total: 0,

    init: function () {
        // I want to run all 3 operations. (with 2 and 3 happening sequentially)
        Promise.all([App.asyncOperation1(1000), App.combine2and3()])
            .then((total) => {
                // then I want to use the result of all 3 operations
                console.log(`total from top: ${total}`);
            })
    },

    asyncOperation1: wait,

    asyncOperation2: wait,

    asyncOperation3: wait,

    combine2and3: async function () {
        var value2 = await App.asyncOperation2(2000);
        var value3 = await App.asyncOperation3(1000);
        console.log(`value2: ${value2}`)
        console.log(`value3: ${value3}`)
        console.log(`summed total ${value2 + value3}`);
        return value2 + value3;
    },
};

App.init();
Hide result
0
source

Source: https://habr.com/ru/post/1696369/


All Articles