Last active
December 30, 2015 00:49
-
-
Save fiveisprime/7751706 to your computer and use it in GitHub Desktop.
Function binding with Async.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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