Execute async code in chronological order. The sequence module of FuturesJS (Browser, Node.js, Bower, and Pakmanager)
JavaScript
Switch branches/tags
Nothing to show
Latest commit 2501c8d Jun 25, 2014 @coolaj86 coolaj86 Update README.md
Permalink
Failed to load latest commit information.
LICENSE v3.0.0 Jan 13, 2014
README.md Update README.md Jun 25, 2014
bower.json updated metadata Jan 13, 2014
package.json updated metadata Jan 13, 2014
sequence.js Fix missing comma Jun 24, 2014
test.js v3.0.0 Jan 13, 2014

README.md

huzzah for es6-promise!

Now that es6-promise exists, Sequence isn't really necessary. Try es6-promise today!

var Promise = require('es6-promise').Promise
  , state = {}
  ;
  
new Promise(function (resolve, reject) {
  resolve('a');
}).then(function (a) {
  state.a = a;
  return new Promise(function (resolve, reject) {
    resolve('b');
  });
}).then(function (b) {
  state.b = b;
  console.log(state);
})

P.S. Sequence is admittedly a bit more intuitive than and I'd happily accept a pull request that uses es6-promise under the hood.

Sequence

Creates an Asynchronous Stack which execute each enqueued method after the previous function calls the provided next(err, data [, ...]).

In many cases forEachAsync or forAllAsync will be a better alternative.

API

  • Sequence.create(defaultContext=null)
  • then(function callback(next, err, data [, ...]) {}, context) - add a callback onto the queue
    • begins or resumes the queue
    • passes the results of the previous function into the next

Node.js Installation

Node.JS (Server):

npm install sequence

Browser Installation

You can install from bower:

bower install sequence

Or download the raw file from https://raw.github.com/FuturesJS/sequence/master/sequence.js:

wget https://raw.github.com/FuturesJS/sequence/master/sequence.js

Or build with pakmanager:

pakmanager build sequence

Usage

(function (exports) {
  'use strict';

  var Sequence = exports.Sequence || require('sequence').Sequence
    , sequence = Sequence.create()
    , err
    ;

  sequence
    .then(function (next) {
      setTimeout(function () {
        next(err, "Hi", "World!");
      }, 120);
    })
    .then(function (next, err, a, b) {
      setTimeout(function () {
        next(err, "Hello", b);
      }, 270);
    })
    .then(function (next, err, a, b) {
      setTimeout(function () {
        console.log(a, b);
        next();
      }, 50);
    });

// so that this example works in browser and node.js
}('undefined' !== typeof exports && exports || new Function('return this')()));