diff --git a/README.md b/README.md index fd6c9a6..91faaf6 100644 --- a/README.md +++ b/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. diff --git a/lib/wrap-async-functions.js b/lib/wrap-async-functions.js index 1854ebe..4fa24b1 100644 --- a/lib/wrap-async-functions.js +++ b/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] || {}; diff --git a/package.json b/package.json index 79e379f..095d502 100644 --- a/package.json +++ b/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" diff --git a/test/api.test.js b/test/api.test.js index 27a163d..9f9a3b2 100644 --- a/test/api.test.js +++ b/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); + }); + }); }); \ No newline at end of file