Make synchronous functions look asynchronous
Make synchronous functions look asynchronous.
Current Version: 1.2.0
Node Support: 0.6, 0.8, 0.10
Browser Support: Android Browser 2.2–4.2, Firefox 3.6, Firefox 4–19, Google Chrome 14–26, Internet Explorer 6–10, Mobile Safari iOS 3–6, Opera 12.10, Safari 5–6
As an oversimplified example, if you have a function which takes an input and returns an output which works like this:
var result = lowercase'FOO';console.logresult; // 'foo'
Asynchronize will turn it into a function which works like this:
lowercase'FOO'console.logresult; // 'foo';
You can use Asynchronize on the server side with Node.js and npm:
$ npm install asynchronize
$ bower install asynchronize$ component install rowanmanning/asynchronize
$ component install rowanmanning/asynchronize
or by simply including
asynchronize.js in your page:
In Node.js or using Component, you can include Asynchronize in your script by using require:
var asynchronize = require'asynchronize';
Asynchronize also works with AMD-style module loaders, just specify it as a dependency.
If you're just including with a
asynchronize is available as a function in the global scope.
Create an async-style version of
fn: (function) The function to convert.
return: (function) Returns the new async-style function.
return valtoLowerCase;var lowercaseAsync = asynchronizelowercase;
The created function accepts any number of arguments, the last one being used as the callback if it's a function. The callback is called with two arguments; an error object (the result of a
throw within the original function), and a result (the return value of the original function).
Let's extend the example to throw errors:
if typeof val !== 'string'throw 'Expected string';return valtoLowerCase;var lowercaseAsync = asynchronizelowercase;// Call with stringlowercaseAsync'FOO'console.logerror; // undefinedconsole.logresult; // 'foo';// Call with non-stringlowercaseAsync123console.logerror; // new Error('Expected string')console.logresult; // undefined;
To develop Asynchronize, you'll need to clone the repo and install dependencies with
make deps. If you're on Windows, you'll also need to install Make for Windows.
Once you're set up, you can run the following commands:
$ make deps # Install dependencies$ make lint # Run JSHint with the correct config$ make test # Run unit tests in Node$ make test-server # Run a server for browser unit testing (visit localhost:3000)
When no build target is specified, make will run
deps lint test. This means you can use the following command for brevity:
Code with lint errors or no/failing tests will not be accepted, please use the build tools outlined above.
Asynchronize is licensed under the MIT license.