Skip to content
Utility method to run function either synchronously or asynchronously using the common `this.async()` style.
JavaScript
Branch: nodeback
Clone or download
Pull request Compare This branch is 7 commits ahead, 29 commits behind SBoudrias:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitattributes
.gitignore
.jshintrc
.travis.yml
LICENSE
README.md
index.js
package.json
test.js

README.md

Run Async Nodeback

npm tests dependencies

DEPRECATED - This is now the default behavior of run-async. Just use that.

Fork of run-async that uses nodeback style callbacks (i.e. callback(err, result));

Utility method to run function either synchronously or asynchronously using the common this.async() style.

This is useful for library author accepting sync or async functions as parameter. runAsync will always run them as async method, and normalize the function handling.

runAsync.dezalgo ensures your callback is always called asynchronously.

Standard runAsync method will run your callback synchronously if user callback is synchronous. (beware of Zalgo!)

Installation

npm install --save run-async

Usage

var runAsync = require('run-async-nodeback');

// In Async mode:
var asyncFn = function (a) {
  var done = this.async();

  setTimeout(function () {
    done(null, 'running: ' + a);
  }, 10);
};

runAsync(asyncFn, function (err, answer) {
  if (err) throw err;
  console.log(answer); // 'running: async'
}, 'async');

// In Sync mode:
var syncFn = function (a) {
  return 'running: ' + a;
};

runAsync(syncFn, function (err, answer) {
  if (err) throw err;
  console.log(answer); // 'running: sync'
}, 'sync');


runAzync.dezalgo(syncFun, function (err, answer) {
  if (err) throw err;
  console.log(answer); // 'running: dezalgo'
}, 'dezalgo');

Licence

Copyright (c) 2014 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.

You can’t perform that action at this time.