-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async.race() method #1018
Closed
Closed
async.race() method #1018
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1090,7 +1090,7 @@ | |
var memoized = _restParam(function memoized(args) { | ||
var callback = args.pop(); | ||
var key = hasher.apply(null, args); | ||
if (has.call(memo, key)) { | ||
if (has.call(memo, key)) { | ||
async.setImmediate(function () { | ||
callback.apply(null, memo[key]); | ||
}); | ||
|
@@ -1247,6 +1247,27 @@ | |
}); | ||
}; | ||
|
||
async.race = function (tasks, callback) { | ||
callback = _once(callback || noop); | ||
if (!_isArray(tasks)) { | ||
return callback(new TypeError('First argument to waterfall must be an array of functions')); | ||
} | ||
if (!tasks.length) { | ||
return callback(null); | ||
} | ||
for (var i = 0; i < tasks.length; i++) { | ||
tasks[i](only_once(done)); | ||
} | ||
var called = false; | ||
function done(err, result) { | ||
if (called) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for |
||
called = true; | ||
callback(err, result); | ||
} | ||
}; | ||
|
||
// Node.js | ||
if (typeof module === 'object' && module.exports) { | ||
module.exports = async; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
var async = require('../lib/async'); | ||
var assert = require('assert'); | ||
|
||
describe('race', function () { | ||
it('should call each function in parallel and callback with first result', function (done) { | ||
var finished = 0; | ||
var tasks = []; | ||
for (var i = 0; i < 10; i++) { | ||
tasks[i] = (function () { | ||
var index = i; | ||
return function (next) { | ||
finished++; | ||
next(null, index); | ||
} | ||
})(); | ||
} | ||
async.race(tasks, function (err, result) { | ||
assert.ifError(err); | ||
//0 finished first | ||
assert.strictEqual(result, 0); | ||
assert.strictEqual(finished, 1); | ||
async.setImmediate(function () { | ||
assert.strictEqual(finished, 10); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should callback with the first error', function (done) { | ||
var tasks = []; | ||
for (var i = 0; i <= 5; i++) { | ||
tasks[i] = (function () { | ||
var index = i; | ||
return function (next) { | ||
setTimeout(function () { | ||
next(new Error('ERR' + index)); | ||
}, 50 - index * 2); | ||
} | ||
})(); | ||
} | ||
async.race(tasks, function (err, result) { | ||
assert.ok(err); | ||
assert.ok(err instanceof Error); | ||
assert.strictEqual(typeof result, 'undefined'); | ||
assert.strictEqual(err.message, 'ERR5'); | ||
done(); | ||
}); | ||
}); | ||
it('should callback when task is empty', function (done) { | ||
async.race([], function (err, result) { | ||
assert.strictEqual(err, null); | ||
assert.strictEqual(typeof result, 'undefined'); | ||
done(); | ||
}); | ||
}); | ||
it('should callback in error the task arg is not an Array', function () { | ||
var errors = []; | ||
async.race(null, function (err) { | ||
errors.push(err); | ||
}); | ||
async.race({}, function (err) { | ||
errors.push(err); | ||
}); | ||
assert.strictEqual(errors.length, 2); | ||
assert.ok(errors[0] instanceof TypeError); | ||
assert.ok(errors[1] instanceof TypeError); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo, change waterfall to race