0

I'm using this function to do a callback, the task is to not modify the 'f' function. The idea is to start all functions(parallel) and then end all functions and display 'Done' at the end.(runing in node).

function f(cb) {
  console.log("f's activity starts.");
  var t = Math.random() * 500; //gives a num between 0 and 1000

  function onActivityDone() {
    console.log("f's activity ends.");
    if (cb) cb();
  }
  setTimeout(onActivityDone, t);

}

function final() {
  console.log('Done');
}

function first() {
  final();
}



f()
{     
f()
{
    f(final)
  };
};

This is how the output is supposed to look.

f's activity starts.

f's activity starts.

f's activity starts.

f's activity ends.

f's activity ends.

f's activity ends.

Done.

Sometimes I got that output but not always, most of the time looks like this

f's activity starts.

f's activity starts.

f's activity starts.

f's activity ends.

f's activity ends.

Done.

f's activity ends.

and I have no idea why :/

Any ideas why..

Thank you !

3
  • 1
    Can you provide a demo? Eyeballing on your code, I don't think it would even run. Commented Dec 28, 2013 at 2:04
  • Note that final is a reserved word in ecmascript 2+:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 28, 2013 at 2:10
  • I try the code and works, but don't display the 'f' in the correct order. Commented Dec 28, 2013 at 2:28

2 Answers 2

1

I guess your last part should look like this:

f( function() {
  f( function() {
    f(final)
  });
});

And the output will be predicted:

f's activity starts. (index):22
f's activity ends. (index):26
f's activity starts. (index):22
f's activity ends. (index):26
f's activity starts. (index):22
f's activity ends. (index):26
Done 

The following code you provided is not even compilable:

f() {
  f() {
    f(final)
  };
};

Maybe you wanted something like this:

f ( f ( f(final)));

But it is incorrect too, because code will be executed in the wrong direction. f(final) will be executed first!

EDIT

If you need to start 3 tasks in parallel use https://github.com/caolan/async#parallel

Sign up to request clarification or add additional context in comments.

6 Comments

Thank for your comment, I try your example and work fine, but on my situation all the fuunction need to start first and then print 'end' for all three function and display 'done' at ver end.. f's activity starts. f's activity starts. f's activity starts. f's activity ends. f's activity ends. f's activity ends. Done. this is how the out is suppose to look but I don't know how, thank for you help :)
Your approach is wrong! Callback should be called after work is done! If you need to start them all and handle competion of them all you need github.com/caolan/async#parallel
I been trying with the link you provide but it didn't work, any idea on how to make the 'f' function work in parallel ?
@user3118337 It should :) Take a look at my example: jsfiddle.net/MJqPY Feel free to ask if you need more info.
A question about your code, that I need to download a lib o something, because when I run the code say async is not defined
|
0

The calls to onActivityDone will not necessarily happen in the same order as the calls to f. This is true regardless of the block structure you have used, which I have never seen done before and only seems to confuse the situation.

setTimeout returns immediately - it is not like a "sleep" function that blocks execution until a certain amount of time has passed, it simply stores the callback and the time to wait and then carries on, and Javascript keeps an internal timer to call the callback when the time comes.

Comments

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.