Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Flow Control in JavaScript

Tags:

javascript

Could you, please, explain to me, how to write really basic flow control in JavaScript? Thank you.

flow([

  function(callback) { /* do something */ callback(); /* run next function */ },
  function(callback) { /* do something */ callback(); /* run next function */ },
  function(callback) { /* do something */ callback(); /* run next function */ },
  function(callback) { /* do something */ callback();  }

], function() {

  alert("Done.");

});
like image 557
Josh Newton Avatar asked Dec 21 '10 01:12

Josh Newton


People also ask

What is control flow in JavaScript?

The control flow is the order in which the computer executes statements in a script. Code is run in order from the first line in the file to the last line, unless the computer runs across the (extremely frequent) structures that change the control flow, such as conditionals and loops.

What is control flow explain with example?

Many programming languages have what are called control flow statements, which determine what section of code is run in a program at any time. An example of a control flow statement is an if/else statement, shown in the following JavaScript example. var x = 1; if (x === 1) {

What are the types of control flow?

There are three basic types of logic, or flow of control, known as: Sequence logic, or sequential flow. Selection logic, or conditional flow. Iteration logic, or repetitive flow.


1 Answers

Would something like this work?

function flow(fns, last) {
    var f = last;
    for (var i = fns.length - 1; i >= 0; i--)
        f = makefunc(fns[i], f);
    f();
}

function makefunc(f, g) {
    return function() { f(g) }
}
like image 71
Anon. Avatar answered Sep 26 '22 14:09

Anon.