Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

run-async

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

run-async - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

27

index.js
'use strict';
var once = require('once');
var isPromise = require('is-promise');

@@ -15,12 +16,22 @@ /**

var async = false;
var answer = func.apply({
async: function () {
async = true;
return once(cb);
cb = once(cb);
try {
var answer = func.apply({
async: function () {
async = true;
return cb;
}
}, Array.prototype.slice.call(arguments, 2) );
if (!async) {
if (isPromise(answer)) {
answer.then(cb.bind(null, null), cb);
} else {
setImmediate(cb.bind(null, null, answer));
}
}
}, Array.prototype.slice.call(arguments, 2) );
if (!async) {
cb(answer);
} catch (e) {
setImmediate(cb.bind(null, e));
}
};
{
"name": "run-async",
"version": "0.1.0",
"version": "1.0.0",
"description": "Utility method to run function either synchronously or asynchronously using the common `this.async()` style.",

@@ -25,7 +25,9 @@ "main": "index.js",

"dependencies": {
"is-promise": "^2.1.0",
"once": "^1.3.0"
},
"devDependencies": {
"bluebird": "^2.10.2",
"mocha": "^1.21.4"
}
}

@@ -6,3 +6,3 @@ Run Async

Utility method to run function either synchronously or asynchronously using the common `this.async()` style.
Utility method to run function either synchronously or asynchronously using the common `this.async()` style. Also provides for promise returning functions.

@@ -29,7 +29,7 @@ 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.

setTimeout(function () {
done('running: ' + a);
done(null, 'running: ' + a);
}, 10);
};
runAsync(asyncFn, function (answer) {
runAsync(asyncFn, function (err, answer) {
console.log(answer); // 'running: async'

@@ -43,5 +43,15 @@ }, 'async');

runAsync(asyncFn, function (answer) {
runAsync(syncFn, function (err, answer) {
console.log(answer); // 'running: sync'
}, 'sync');
var promiseFunc = function(a) {
return new Promise(function (resolve, reject) {
resolve('running: ' + a);
});
}
runAsync(promiseFunc, function (err, answer) {
console.log(answer); // 'running: promise'
}, 'promise');
```

@@ -48,0 +58,0 @@

@@ -5,12 +5,17 @@ 'use strict';

var runAsync = require('./index');
var Promise = require('bluebird');
describe('runAsync', function () {
it('run synchronous method', function (done) {
var ranAsync = false;
var aFunc = function () {
return 'pass1';
};
runAsync(aFunc, function (val) {
runAsync(aFunc, function (err, val) {
assert.ifError(err);
assert(ranAsync);
assert.equal(val, 'pass1');
done();
});
ranAsync = true;
});

@@ -21,6 +26,7 @@

var returns = this.async();
setTimeout(returns.bind(null, 'pass2'), 0);
setImmediate(returns.bind(null, null, 'pass2'));
};
runAsync(aFunc, function (val) {
runAsync(aFunc, function (err, val) {
assert.ifError(err);
assert.equal(val, 'pass2');

@@ -37,3 +43,4 @@ done();

};
runAsync(aFunc, function (val) {
runAsync(aFunc, function (err, val) {
assert.ifError(err);
done();

@@ -50,6 +57,51 @@ }, 1, 'bar');

runAsync(aFunc, function (val) {
runAsync(aFunc, function (err, val) {
assert.ifError(err);
done();
});
});
it('handles promises', function (done) {
var fn = function () {
return new Promise(function (resolve, reject) {
setImmediate(function () {
resolve('as promised!');
});
});
};
runAsync(fn, function (err, val) {
assert.ifError(err);
assert.equal('as promised!', val);
done();
});
});
it('throwing synchronously passes error to callback', function (done) {
var throws = function () {
throw new Error('sync error');
};
runAsync(throws, function (err, val) {
assert(err);
assert.equal(err.message, 'sync error');
done();
});
});
it('rejecting a promise passes error to callback', function (done) {
var rejects = function () {
return new Promise(function (resolve, reject) {
setImmediate(function () {
reject(new Error('broken promise'));
});
});
};
runAsync(rejects, function (err, val) {
assert(err);
assert.equal(err.message, 'broken promise');
done();
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc