Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Async functions should now return a value
Browse files Browse the repository at this point in the history
  • Loading branch information
dallonf committed Nov 5, 2012
1 parent e2fc656 commit 5457f36
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# async-eval

Execute arbitrary JS with callbacks in node.js
Execute arbitrary JS with callbacks in node.js. Also counts asynchronous operations and does not return until all callbacks have been executed.

Note: This library actually uses `vm.runInNewContext()` instead of `eval()` for a bit more added security, though it doesn't fork a process, so it's best used with trusted code.

Expand Down
2 changes: 1 addition & 1 deletion lib/wrap-async-functions.js
Expand Up @@ -30,7 +30,7 @@ function wrapAsyncFunctions(asyncFunctions, sandbox, events, done, sandboxRoot)
events.emit('finishCallback');
});
}
asyncFunctions[k].apply(sandboxRoot._this, args);
return asyncFunctions[k].apply(sandboxRoot._this, args);
};
} else if (typeof asyncFunctions[k] === 'object') {
sandbox[k] = sandbox[k] || {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Dallon Feldner",
"name": "async-eval",
"description": "Execute arbitrary JS with callbacks",
"version": "0.1.4",
"version": "0.1.5",
"repository": {
"type": "git",
"url": "git://github.com/dallonf/async-eval.git"
Expand Down
32 changes: 28 additions & 4 deletions test/api.test.js
@@ -1,3 +1,4 @@
/*jshint expr:true*/
var asyncEval = require('../');
var expect = require('chai').expect;

Expand Down Expand Up @@ -39,7 +40,7 @@ describe('asyncEval', function() {
, {this: self, asyncFunctions: {wait: wait}}, function() {
expect(self.x).to.equal(5);
done();
})
});
});

it('should return a syntax error', function(done) {
Expand Down Expand Up @@ -94,7 +95,7 @@ describe('asyncEval', function() {
it('should return a runtime error from a callback', function(done) {

var test = function() {
var foo = undefined;
var foo;
wait(function() {
foo.toString();
});
Expand All @@ -109,7 +110,7 @@ describe('asyncEval', function() {
it('should return a valid runtime error from a callback', function(done) {

var test = function() {
var foo = undefined;
var foo;
wait(function() {
foo.toString();
});
Expand Down Expand Up @@ -142,7 +143,7 @@ describe('asyncEval', function() {
this.x += 10;
wait.fifty(function() {
this.x += 50;
})
});
});
};

Expand All @@ -154,4 +155,27 @@ describe('asyncEval', function() {
});

});

it('should return the value of an async function', function(done) {
var async = {
waitAndReturn: function(callback) {
setTimeout(callback, 2);
return 2;
}
};

var test = function() {
this.x = waitAndReturn(function() {
this.y = 1;
});
};

var self = {};

asyncEval(funcToString(test), {asyncFunctions: async, this: self}, function(err) {
expect(self.y).to.equal(1);
expect(self.x).to.equal(2);
done(err);
});
});
});

0 comments on commit 5457f36

Please sign in to comment.