Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Async functions should now return a value

  • Loading branch information...
commit 5457f366142af979020373c5d5e6e70d0d1be86c 1 parent e2fc656
@dallonf authored
View
2  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.
View
2  lib/wrap-async-functions.js
@@ -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] || {};
View
2  package.json
@@ -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"
View
32 test/api.test.js
@@ -1,3 +1,4 @@
+/*jshint expr:true*/
var asyncEval = require('../');
var expect = require('chai').expect;
@@ -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) {
@@ -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();
});
@@ -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();
});
@@ -142,7 +143,7 @@ describe('asyncEval', function() {
this.x += 10;
wait.fifty(function() {
this.x += 50;
- })
+ });
});
};
@@ -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);
+ });
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.