Skip to content

Instantly share code, notes, and snippets.

@fiveisprime
Last active December 30, 2015 00:49
Show Gist options
  • Save fiveisprime/7751706 to your computer and use it in GitHub Desktop.
Save fiveisprime/7751706 to your computer and use it in GitHub Desktop.
Function binding with Async.
var async = require('async');
//
// Constructor function.
//
function Test() {}
//
// Start the async operation.
//
// This isn't the best example, but shows how you can scope
// function calls in async without using `var self = this;`
//
// Output: `{ '0': null, '1': [ 'one', 'two' ] }`
//
Test.prototype.go = function() {
async.series([
function(fn) {
return this.one(fn);
}.bind(this),
function(fn) {
return this.two(fn);
}.bind(this),
],
function() {
console.log(arguments);
});
};
//
// Simulated async operations
// ==========================
//
//
// One.
//
Test.prototype.one = function(fn) {
return fn(null, 'one');
};
//
// Two.
//
Test.prototype.two = function(fn) {
return fn(null, 'two');
};
//
// Instantiate a new Test object then start the async operation.
//
var test = new Test();
test.go();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment