Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Execute arbitrary JS with callbacks.
JavaScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
.gitignore
README.md
index.js
package.json

README.md

async-eval

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.

Installation

npm install async-eval
npm test

or

git clone https://github.com/dallonf/async-eval
npm install
npm test 

Example usage

var asyncEval = require('async-eval');

var someObject = {x: 5, y: 10};

function waitOneSecond(callback) {
  setTimeout(callback, 1000);
}

var options = {
  this: someObject,
  asyncFunctions: {
    waitOneSecond: waitOneSecond
  }
}

asyncEval('waitOneSecond(function() { this.x += 2; });', options, function() {
  console.log(someObject.x); // 7
});

Usage

asyncEval(code, [options], [callback])

asyncEval() will interpret and execute code and run callback when the code and every asynchronous function it calls has finished running.

Options

this

Default: {}

Sets the object that will be used as this in the executed code and any nested callbacks.

context

Default: {}

Sets the global context in the executed code. Put any synchronous DSL functions and global variables here.

asyncFunctions

Default: {}

Registers asynchronous functions into the context. Asynchronous functions must be listed in the asyncFunctions property so that asyncEval can count pending callbacks.

The functions registered in asyncFunctions must take a callback as the last argument.

These functions can be namespaced with objects, for example:

asyncFunctions: {
  users: {
    get: function(callback) { /* ... */ },
    create: function(user, callback) { /* ... */ },
  },
  posts: {
    get: function(callback) { /* ... */ },
    create: function(post, callback) { /* ... */ },
  }
}
Something went wrong with that request. Please try again.