Skip to content

Commit

Permalink
feat: make async callbacks call in a new event cycle (fixes #22)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 7, 2016
1 parent 83e1ffc commit eeb7516
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,15 @@ MemoryFileSystem.prototype.createWriteStream = function(path, options) {
try {
var result = this[fn + "Sync"](path);
} catch(e) {
return callback(e);
process.nextTick(function() {
callback(e);
});

return;
}
return callback(null, result);
process.nextTick(function() {
callback(null, result);
});
};
});

Expand All @@ -293,9 +299,15 @@ MemoryFileSystem.prototype.createWriteStream = function(path, options) {
try {
var result = this[fn + "Sync"](path, optArg);
} catch(e) {
return callback(e);
process.nextTick(function() {
callback(e);
});

return;
}
return callback(null, result);
process.nextTick(function() {
callback(null, result);
});
};
});

Expand Down
22 changes: 22 additions & 0 deletions test/MemoryFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,28 @@ describe("errors", function() {
});
});
describe("async", function() {
["stat", "readdir", "mkdirp", "rmdir", "unlink", "readlink"].forEach(function(methodName) {
it("should call " + methodName + " callback in a new event cycle", function(done) {
var fs = new MemoryFileSystem();
var isCalled = false;
fs[methodName]('/test', function() {
isCalled = true;
done();
});
should(isCalled).eql(false);
});
});
["mkdir", "readFile"].forEach(function(methodName) {
it("should call " + methodName + " a callback in a new event cycle", function(done) {
var fs = new MemoryFileSystem();
var isCalled = false;
fs[methodName]('/test', {}, function() {
isCalled = true;
done();
});
should(isCalled).eql(false);
});
});
it("should be able to use the async versions", function(done) {
var fs = new MemoryFileSystem();
fs.mkdirp("/test/dir", function(err) {
Expand Down

0 comments on commit eeb7516

Please sign in to comment.