Home » Javascript » How to execute javascript functions synchronously

How to execute javascript functions synchronously

Posted by: admin August 19, 2018 Leave a comment

Questions:

I am newbie in nodejs. Can some write me a sudo code that does the following?

Function1(); //returns an array which can be used in function2 and function3

Function2(); //returns an array which can be used in function3

Function3();

I want to run all three functions synchronously.
So function2 has to wait for function1 to finish, then use the returned array in the function2. Then function3 waits for function2 to finish, then use the returned array in the function3 and so forth.

Tried something like this but then seems execute at the same time as well.

function main() {
    return Promise.resolve()
        .then (function(){
            function1()
        })
        .then (function(){
            function2()
        })
        .then (function(){
            function3()
        })
}
Answers: