Skip to content

Commit

Permalink
Removing _.delay ... making _.defer equivalent to the previous proces…
Browse files Browse the repository at this point in the history
…s.nextTick
  • Loading branch information
jashkenas committed Dec 8, 2010
1 parent 2b69c99 commit 3f94fc7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
20 changes: 10 additions & 10 deletions test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ if (typeof require !== 'undefined') {

(function (exports) {

exports['async: nextTick'] = function(test){
exports['async: defer'] = function(test){
var call_order = [];
_.nextTick(function(){call_order.push('two');});
_.defer(function(){call_order.push('two');});
call_order.push('one');
setTimeout(function(){
test.same(call_order, ['one','two']);
test.done();
}, 50);
};

exports['async: nextTick in node'] = function(test){
exports['async: defer in node'] = function(test){
test.expect(1);
var browser = false;
if (typeof process === 'undefined') {
Expand All @@ -32,10 +32,10 @@ exports['async: nextTick in node'] = function(test){
test.ok(true, 'process.nextTick called');
test.done();
};
_.nextTick(function(){});
_.defer(function(){});
};

exports['async: nextTick in the browser'] = function(test){
exports['async: defer in the browser'] = function(test){
test.expect(1);

if (typeof process !== 'undefined') {
Expand All @@ -44,7 +44,7 @@ exports['async: nextTick in the browser'] = function(test){
}

var call_order = [];
_.nextTick(function(){call_order.push('two');});
_.defer(function(){call_order.push('two');});

call_order.push('one');
setTimeout(function(){
Expand Down Expand Up @@ -210,7 +210,7 @@ exports['async: asyncReduce'] = function(test){

exports['async: asyncReduce async with non-reference memo'] = function(test){
_.asyncReduce([1,3,2], 0, function(a, x, callback){
setTimeout(function(){callback(null, a + x)}, Math.random()*100);
setTimeout(function(){ callback(null, a + x); }, Math.random()*100);
}, function(err, result){
test.equals(result, 6);
test.done();
Expand Down Expand Up @@ -557,7 +557,7 @@ exports['async: parallel error'] = function(test){
exports['async: parallel no callback'] = function(test){
_.parallel([
function(callback){callback();},
function(callback){callback(); test.done();},
function(callback){callback(); test.done();}
]);
};

Expand Down Expand Up @@ -653,7 +653,7 @@ exports['async: series error'] = function(test){
exports['async: series no callback'] = function(test){
_.series([
function(callback){callback();},
function(callback){callback(); test.done();},
function(callback){callback(); test.done();}
]);
};

Expand Down Expand Up @@ -1068,7 +1068,7 @@ exports['async: asyncWhile'] = function (test) {
['iterator', 1], ['test', 2],
['iterator', 2], ['test', 3],
['iterator', 3], ['test', 4],
['iterator', 4], ['test', 5],
['iterator', 4], ['test', 5]
]);
test.equals(count, 5);
test.done();
Expand Down
16 changes: 4 additions & 12 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,11 @@ exports["functions: memoize"] = function(test) {
test.done();
};

exports["functions: delay"] = function(test) {
test.expect(2);
var delayed = false;
_.delay(function(){ delayed = true; }, 100);
_.delay(function(){ test.ok(!delayed, "didn't delay the function quite yet"); }, 50);
_.delay(function(){ test.ok(delayed, 'delayed the function'); test.done(); }, 150);
};

exports["functions: defer"] = function(test) {
test.expect(1);
var deferred = false;
_.defer(function(bool){ deferred = bool; }, true);
_.delay(function(){ test.ok(deferred, "deferred the function"); test.done(); }, 50);
_.defer(function(){ deferred = true; });
setTimeout(function(){ test.ok(deferred, "deferred the function"); test.done(); }, 50);
};

exports["functions: throttle"] = function(test) {
Expand All @@ -87,7 +79,7 @@ exports["functions: throttle"] = function(test) {
setTimeout(throttledIncr, 70);
setTimeout(throttledIncr, 110);
setTimeout(throttledIncr, 120);
_.delay(function(){ test.ok(counter == 3, "incr was throttled"); test.done(); }, 180);
setTimeout(function(){ test.ok(counter == 3, "incr was throttled"); test.done(); }, 180);
};

exports["functions: debounce"] = function(test) {
Expand All @@ -101,7 +93,7 @@ exports["functions: debounce"] = function(test) {
setTimeout(debouncedIncr, 90);
setTimeout(debouncedIncr, 120);
setTimeout(debouncedIncr, 150);
_.delay(function(){ test.ok(counter == 1, "incr was debounced"); test.done(); }, 220);
setTimeout(function(){ test.ok(counter == 1, "incr was debounced"); test.done(); }, 220);
};

exports["functions: wrap"] = function(test) {
Expand Down
30 changes: 9 additions & 21 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,15 @@
};
};

// Delays a function for the given number of milliseconds, and then calls
// it with the arguments supplied.
_.delay = function(func, wait) {
var args = slice.call(arguments, 2);
return setTimeout(function(){ return func.apply(func, args); }, wait);
};

// Defers a function, scheduling it to run after the current call stack has
// cleared.
// cleared. If we're running on Node.js, this is implemented with `nextTick`,
// otherwise, use a `setTimeout`.
_.defer = function(func) {
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
if (typeof process !== 'undefined' && process.nextTick) {
process.nextTick(func);
} else {
setTimeout(func, 0);
}
};

// Internal function used to implement `_.throttle` and `_.debounce`.
Expand Down Expand Up @@ -700,16 +698,6 @@
// Start of async.js merge - Caolan McMahon (@caolan)
// --------------------------------------------------

//// nextTick implementation with browser-compatible fallback ////
_.nextTick = function (fn) {
if (typeof process === 'undefined' || !(process.nextTick)) {
setTimeout(fn, 0);
}
else {
process.nextTick(fn);
}
};

_.asyncEach = _.asyncForEach = function (arr, iterator, callback) {
if (!arr.length) {
return callback();
Expand Down Expand Up @@ -1075,7 +1063,7 @@
else {
args.push(callback);
}
_.nextTick(function () {
_.defer(function () {
iterator.apply(null, args);
});
}
Expand Down Expand Up @@ -1107,7 +1095,7 @@
concurrency: concurrency,
push: function (data, callback) {
tasks.push({data: data, callback: callback});
_.nextTick(q.process);
_.defer(q.process);
},
process: function () {
if (workers < q.concurrency && tasks.length) {
Expand Down

0 comments on commit 3f94fc7

Please sign in to comment.