Last active
December 15, 2015 22:39
-
-
Save gimenete/5335008 to your computer and use it in GitHub Desktop.
Comparing Q.js and async.js
This file contains 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 Q = require('q') | |
var async = require('async') | |
function dumbQ(value) { | |
var deferred = Q.defer() | |
setTimeout(function() { | |
deferred.resolve(value) | |
}, 200) | |
return deferred.promise | |
} | |
function dumbA(value, callback) { | |
setTimeout(function() { | |
return callback(null, value) | |
}, 200) | |
} | |
function callOneFunctionUsingQ() { | |
dumbQ('a').then(function (a) { | |
console.log('a', a.valueOf()) | |
}) | |
} | |
function callOneFunctionUsingA() { | |
dumbA('a', function(err, a) { | |
console.log('a', a) | |
}) | |
} | |
function multipleFunctionsUsingQ() { | |
var values = [dumbQ('a'), dumbQ('b')] | |
Q.allResolved(values).then(function (results) { | |
var c = results[0].valueOf() + results[1].valueOf() | |
console.log('c', c) | |
}) | |
} | |
function multipleFunctionsUsingA() { | |
var values = ['a', 'b'] | |
async.map(values, dumbA, function(err, results) { | |
var c = results[0] + results[1] | |
console.log('c', c) | |
}) | |
} | |
// callOneFunctionUsingQ() | |
// callOneFunctionUsingA() | |
// multipleFunctionsUsingQ() | |
// multipleFunctionsUsingA() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment