0

I have easy noob question concerning control flow of basic app. I have 3 functions with setTimeout inside.

console.log("[+] Program start");

function first(){
setTimeout(function(){
  console.log(1);
},3000);}

function second(){
setTimeout(function(){
  console.log(2);
},2000);}

function third(){
setTimeout(function(){
  console.log(3);  
},1000);}

first();
second();
third();

console.log("done");

Output is as expected this:

[+] Program start
done
3
2
1

I would like to control flow that I will see things in following order:

[+] Program start
1
2
3
done

So I rewrote program by following way:

console.log("[+] Program start");

function first(){
setTimeout(function(){
  console.log(1);
  second();
},3000);}

function second(){
setTimeout(function(){
  console.log(2);
  third();
},2000);}

function third(){
setTimeout(function(){
  console.log(3);  
  call();
},1000);}

first();

function call(){console.log("done ");}

Output is:

[+] Program start
1
2
3
done 

Now output is ok, I would like to ask you, is this approach right? It this right way how to control flow or how to write in node.js? Or I am totally on wrong way. Could you please check it and give me some hints, advices etc. Thank you very much for help.

2
  • 1
    Why do you want to use setTimeout? what are you wanting to do? Tell us more about your question. Without that your question is too broad to give you a valid answer Commented Feb 3, 2017 at 19:57
  • What you're doing is pretty weird to say the least. For general async things tho, checkout: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Feb 3, 2017 at 19:58

1 Answer 1

2

If you're trying to manage the order of operations, you definitely don't want to be using setTimeout() to do it. setTimeout() is designed to block the thread from executing until done, and used this way would make your application extraordinarily fragile.

Assuming you are just trying to manage the flow, read up on how Node.js’s event loop works. Your code, written in that fashion, would look like this (I've added in some setTimeout() functions in there to simulate/illustrate long-running functions):

console.log("[+] Program start")

var first = function(callback) {
    setTimeout(function() {
        console.log(1)
        callback()
    }, 1000)
}

var second = function(callback) {
    setTimeout(function() {
        console.log(2)
        callback()
    }, 1000)
}

var third = function(callback) {
    setTimeout(function() {
        console.log(3)
        callback()
    }, 1000)
}


first(function() {
    second(function() {
        third(function() {
            console.log("done ")        
        })
    })
})

Having said that, there's an inherent downside to using callbacks in a nested fashion like this: "callback hell". You may consider modularizing your functions, using something like async's waterfall, or a Promise library instead.

Bonus points: You could also write the function chain in ES6 quite a bit more concisely:

first(() => second(() => third(() =>
    console.log("done ")        
)))
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I used setTimeout only for test, i tried to simulate some work which run for limited period of time. That could be read of file or something like that. My intention is to know how to control flow. Thank you very much.
OK - I added some into the mix for illustration purposes.
Thank you Brandon, finally I am starting to understand phrase callback hell:) It would be nice to see your solution with async library. Anyway thank you very much:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.