Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit a542294

Browse files
authored
feat: make async callbacks call in a new event cycle (fixes #22) (#27)
1 parent 0a12b5c commit a542294

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

lib/MemoryFileSystem.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,15 @@ MemoryFileSystem.prototype.createWriteStream = function(path, options) {
278278
try {
279279
var result = this[fn + "Sync"](path);
280280
} catch(e) {
281-
return callback(e);
281+
process.nextTick(function() {
282+
callback(e);
283+
});
284+
285+
return;
282286
}
283-
return callback(null, result);
287+
process.nextTick(function() {
288+
callback(null, result);
289+
});
284290
};
285291
});
286292

@@ -293,9 +299,15 @@ MemoryFileSystem.prototype.createWriteStream = function(path, options) {
293299
try {
294300
var result = this[fn + "Sync"](path, optArg);
295301
} catch(e) {
296-
return callback(e);
302+
process.nextTick(function() {
303+
callback(e);
304+
});
305+
306+
return;
297307
}
298-
return callback(null, result);
308+
process.nextTick(function() {
309+
callback(null, result);
310+
});
299311
};
300312
});
301313

test/MemoryFileSystem.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ describe("errors", function() {
204204
});
205205
});
206206
describe("async", function() {
207+
["stat", "readdir", "mkdirp", "rmdir", "unlink", "readlink"].forEach(function(methodName) {
208+
it("should call " + methodName + " callback in a new event cycle", function(done) {
209+
var fs = new MemoryFileSystem();
210+
var isCalled = false;
211+
fs[methodName]('/test', function() {
212+
isCalled = true;
213+
done();
214+
});
215+
should(isCalled).eql(false);
216+
});
217+
});
218+
["mkdir", "readFile"].forEach(function(methodName) {
219+
it("should call " + methodName + " a callback in a new event cycle", function(done) {
220+
var fs = new MemoryFileSystem();
221+
var isCalled = false;
222+
fs[methodName]('/test', {}, function() {
223+
isCalled = true;
224+
done();
225+
});
226+
should(isCalled).eql(false);
227+
});
228+
});
207229
it("should be able to use the async versions", function(done) {
208230
var fs = new MemoryFileSystem();
209231
fs.mkdirp("/test/dir", function(err) {

0 commit comments

Comments
 (0)