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 !
finalis a reserved word in ecmascript 2+:developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…