tasq

yet another javascript library to manage control flow

npm install tasq
52 downloads in the last month

Tasq

yet another javascript library to manage control flow

It's available on both node and browser

Why flow library - nested callback

// traditional styles
a1(data, function(r1) {
  // some magic
  a2(r1, function(r2) {
      // some magic
      a3(r2, function(r3) {
        // some magic
      a4(r3, function() {
        // ...
      }
      }
  }
})

This is how people usually mitigate aforementioned issues

function a1(input) {
    var result = input + ' is';
    return a2(result);
}

function a2(input) {
    var result = input + ' a';
    return a3(result);
}

function a3(input) {
    var result = input + ' nice';
    return a4(result);
}

function a4(input) {
    var result = input + ' person';
    return result;
}

// Tom is a nice person
console.log(a1('Tom'));

It's not easy to consume nested callback by a human brain

In order to make async calls with ease Tasq comes to the rescue

Also, there are tons of control flow libraries in the world which supports Promise/A+

Tasq.queue

Running tasks in given order

function first(data) {
  return data + 5;
}

function second(data) {
  return data * 2;
}

function third(data) {
  return data + 3;
}

function last(data) {
  assert.equal(33, data);
}

var task = Tasq.queue([first, second, third, last]);
task(10);

Should be bound to given context

var a = { name: 'A' },
    b = { name: 'B' },
    c = { name: 'C' },
    say = function() { return this.name; },
    task;

task = Tasq.queue([say.bind(a)]);
task();                // 'A'
task.call(a);        // 'A'

task = Tasq.queue([say.bind(b)]);
task();                // 'B'
task.call(b);        // 'B'

task = Tasq.queue([say]);
task.call(c);        // 'C'
task();                // undefined

How Tasq.queue works

Tasq.queue execution

Tasq.async

Running tasks asynchronously

function a() {
  console.log('A');
}

function b() {
  console.log('B');
}

function c() {
  console.log('C');
}

// `callback` is invoked when everything is done successfully
Tasq.async([a, b, c], function callback() {
  console.log("Congrats! Everything is done and let's drink a beer!");
});

Bind context and arguments if indeed

function bindContext() {
  // hello huang47
  console.log('hello ' + this.name);
}

function bindNothing() {
  // hello undefined
  console.log('hello ' + this.name);
}

var o = { name: 'huang47' };

// `callback` is invoked when everything is done successfully
Tasq.async([bindContext.bind(o), bindNothing], function callback() {
  // done
});

How Tasq.async works

Tasq.async execution

YOU ARE WARNED! USE IT AT YOUR OWN RISK

Appendix

npm: tasq

dependencies

unit test

npm: mocha

documentation

npm: yuidocjs

assets are generated from draw.io

npm loves you