summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaolan McMahon <caolan.mcmahon@gmail.com>2014-03-28 14:55:00 +0000
committerCaolan McMahon <caolan.mcmahon@gmail.com>2014-03-28 14:55:00 +0000
commite5ae810dbe4ee82701c947bc4cf5425d0bedeef1 (patch)
tree71571773047d8afc0272b0860a7034fbb66bd794
parent256fe2832d349cb62ef21f1f8e65d5c4cb963589 (diff)
parent5d16b9133793f288e1d98d530ae5665c271b7ace (diff)
downloadasync-e5ae810dbe4ee82701c947bc4cf5425d0bedeef1.tar.gz
Merge pull request #415 from rmg/async-memoize
Make async.memoize() preserve async nature of function
-rwxr-xr-xlib/async.js4
-rwxr-xr-xtest/test-async.js88
2 files changed, 67 insertions, 25 deletions
diff --git a/lib/async.js b/lib/async.js
index 8b00943..1a3d464 100755
--- a/lib/async.js
+++ b/lib/async.js
@@ -857,7 +857,9 @@
var callback = args.pop();
var key = hasher.apply(null, args);
if (key in memo) {
- callback.apply(null, memo[key]);
+ async.nextTick(function () {
+ callback.apply(null, memo[key]);
+ });
}
else if (key in queues) {
queues[key].push(callback);
diff --git a/test/test-async.js b/test/test-async.js
index 3d62bf8..d5b5508 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -2283,23 +2283,63 @@ exports['memoize'] = function (test) {
var call_order = [];
var fn = function (arg1, arg2, callback) {
- call_order.push(['fn', arg1, arg2]);
- callback(null, arg1 + arg2);
+ async.setImmediate(function () {
+ call_order.push(['fn', arg1, arg2]);
+ callback(null, arg1 + arg2);
+ });
};
var fn2 = async.memoize(fn);
fn2(1, 2, function (err, result) {
test.equal(result, 3);
+ fn2(1, 2, function (err, result) {
+ test.equal(result, 3);
+ fn2(2, 2, function (err, result) {
+ test.equal(result, 4);
+ test.same(call_order, [['fn',1,2], ['fn',2,2]]);
+ test.done();
+ });
+ });
});
+};
+
+exports['memoize maintains asynchrony'] = function (test) {
+ test.expect(3);
+ var call_order = [];
+
+ var fn = function (arg1, arg2, callback) {
+ call_order.push(['fn', arg1, arg2]);
+ async.setImmediate(function () {
+ call_order.push(['cb', arg1, arg2]);
+ callback(null, arg1 + arg2);
+ });
+ };
+
+ var fn2 = async.memoize(fn);
fn2(1, 2, function (err, result) {
test.equal(result, 3);
- });
- fn2(2, 2, function (err, result) {
- test.equal(result, 4);
- });
-
- test.same(call_order, [['fn',1,2], ['fn',2,2]]);
- test.done();
+ fn2(1, 2, function (err, result) {
+ test.equal(result, 3);
+ async.nextTick(memoize_done);
+ call_order.push('tick3');
+ });
+ call_order.push('tick2');
+ });
+ call_order.push('tick1');
+
+ function memoize_done() {
+ var async_call_order = [
+ ['fn',1,2], // initial async call
+ 'tick1', // async caller
+ ['cb',1,2], // async callback
+ // ['fn',1,2], // memoized // memoized async body
+ 'tick2', // handler for first async call
+ // ['cb',1,2], // memoized // memoized async response body
+ 'tick3' // handler for memoized async call
+ ];
+ test.same(call_order, async_call_order);
+ test.done();
+ }
};
exports['unmemoize'] = function(test) {
@@ -2308,24 +2348,24 @@ exports['unmemoize'] = function(test) {
var fn = function (arg1, arg2, callback) {
call_order.push(['fn', arg1, arg2]);
- callback(null, arg1 + arg2);
+ async.setImmediate(function () {
+ callback(null, arg1 + arg2);
+ });
};
var fn2 = async.memoize(fn);
var fn3 = async.unmemoize(fn2);
fn3(1, 2, function (err, result) {
test.equal(result, 3);
+ fn3(1, 2, function (err, result) {
+ test.equal(result, 3);
+ fn3(2, 2, function (err, result) {
+ test.equal(result, 4);
+ test.same(call_order, [['fn',1,2], ['fn',1,2], ['fn',2,2]]);
+ test.done();
+ });
+ });
});
- fn3(1, 2, function (err, result) {
- test.equal(result, 3);
- });
- fn3(2, 2, function (err, result) {
- test.equal(result, 4);
- });
-
- test.same(call_order, [['fn',1,2], ['fn',1,2], ['fn',2,2]]);
-
- test.done();
}
exports['unmemoize a not memoized function'] = function(test) {
@@ -2385,11 +2425,11 @@ exports['memoize custom hash function'] = function (test) {
});
fn2(1, 2, function (err, result) {
test.equal(result, 3);
+ fn2(2, 2, function (err, result) {
+ test.equal(result, 3);
+ test.done();
+ });
});
- fn2(2, 2, function (err, result) {
- test.equal(result, 3);
- });
- test.done();
};
exports['memoize manually added memo value'] = function (test) {