pax_global_header00006660000000000000000000000064130202020260014475gustar00rootroot0000000000000052 comment=23767c9d7eaf6a6bb1241fc9e12776685258c50e run-async-2.3.0/000077500000000000000000000000001302020202600134165ustar00rootroot00000000000000run-async-2.3.0/.editorconfig000066400000000000000000000002761302020202600161000ustar00rootroot00000000000000# editorconfig.org root = true [*] indent_style = space indent_size = 2 charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.md] trim_trailing_whitespace = false run-async-2.3.0/.gitattributes000066400000000000000000000000141302020202600163040ustar00rootroot00000000000000* text=auto run-async-2.3.0/.gitignore000066400000000000000000000000161302020202600154030ustar00rootroot00000000000000node_modules/ run-async-2.3.0/.jshintrc000066400000000000000000000005421302020202600152440ustar00rootroot00000000000000{ "node": true, "esnext": true, "bitwise": false, "curly": false, "eqeqeq": true, "eqnull": true, "immed": true, "latedef": false, "newcap": true, "noarg": true, "undef": true, "strict": true, "trailing": true, "smarttabs": true, "indent": 2, "quotmark": "single", "scripturl": true, "globals": [ "describe", "it" ] } run-async-2.3.0/.travis.yml000066400000000000000000000001061302020202600155240ustar00rootroot00000000000000sudo: false language: node_js node_js: - v0.12 - v4 - v5 - v6 run-async-2.3.0/LICENSE000066400000000000000000000020711302020202600144230ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Simon Boudrias Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. run-async-2.3.0/README.md000066400000000000000000000041001302020202600146700ustar00rootroot00000000000000Run Async ========= [![npm](https://badge.fury.io/js/run-async.svg)](http://badge.fury.io/js/run-async) [![tests](https://travis-ci.org/SBoudrias/run-async.svg?branch=master)](http://travis-ci.org/SBoudrias/run-async) [![dependencies](https://david-dm.org/SBoudrias/run-async.svg?theme=shields.io)](https://david-dm.org/SBoudrias/run-async) Utility method to run a function either synchronously or asynchronously using a series of common patterns. This is useful for library author accepting sync or async functions as parameter. `runAsync` will always run them as an async method, and normalize the multiple signature. Installation ========= ```bash npm install --save run-async ``` Usage ========= Here's a simple example print the function results and three options a user can provide a function. ```js var runAsync = require('run-async'); var printAfter = function (func) { var cb = function (err, returnValue) { console.log(returnValue); }; runAsync(func, cb)(/* arguments for func */); }; ``` #### Using `this.async` ```js printAfter(function () { var done = this.async(); setTimeout(function () { done(null, 'done running with callback'); }, 10); }); ``` #### Returning a promise ```js printAfter(function () { return new Promise(function (resolve, reject) { resolve('done running with promises'); }); }); ``` #### Synchronous function ```js printAfter(function () { return 'done running sync function'; }); ``` ### runAsync.cb `runAsync.cb` supports all the function types that `runAsync` does and additionally a traditional **callback as the last argument** signature: ```js var runAsync = require('run-async'); // IMPORTANT: The wrapped function must have a fixed number of parameters. runAsync.cb(function(a, b, cb) { cb(null, a + b); }, function(err, result) { console.log(result) })(1, 2) ``` If your version of node support Promises natively (node >= 0.12), `runAsync` will return a promise. Example: `runAsync(func)(arg1, arg2).then(cb)` Licence ======== Copyright (c) 2014 Simon Boudrias (twitter: @vaxilart) Licensed under the MIT license. run-async-2.3.0/index.js000066400000000000000000000027641302020202600150740ustar00rootroot00000000000000'use strict'; var isPromise = require('is-promise'); /** * Return a function that will run a function asynchronously or synchronously * * example: * runAsync(wrappedFunction, callback)(...args); * * @param {Function} func Function to run * @param {Function} cb Callback function passed the `func` returned value * @return {Function(arguments)} Arguments to pass to `func`. This function will in turn * return a Promise (Node >= 0.12) or call the callbacks. */ var runAsync = module.exports = function (func, cb) { cb = cb || function () {}; return function () { var async = false; var args = arguments; var promise = new Promise(function (resolve, reject) { var answer = func.apply({ async: function () { async = true; return function (err, value) { if (err) { reject(err); } else { resolve(value); } }; } }, Array.prototype.slice.call(args)); if (!async) { if (isPromise(answer)) { answer.then(resolve, reject); } else { resolve(answer); } } }); promise.then(cb.bind(null, null), cb); return promise; } }; runAsync.cb = function (func, cb) { return runAsync(function () { var args = Array.prototype.slice.call(arguments); if (args.length === func.length - 1) { args.push(this.async()); } return func.apply(this, args); }, cb); }; run-async-2.3.0/package.json000066400000000000000000000011351302020202600157040ustar00rootroot00000000000000{ "name": "run-async", "version": "2.3.0", "description": "Utility method to run function either synchronously or asynchronously using the common `this.async()` style.", "main": "index.js", "scripts": { "test": "mocha -R spec" }, "engines": { "node": ">=0.12.0" }, "repository": "SBoudrias/run-async", "keywords": [ "flow", "flow-control", "async" ], "files": [ "index.js" ], "author": "Simon Boudrias ", "license": "MIT", "dependencies": { "is-promise": "^2.1.0" }, "devDependencies": { "mocha": "^3.1.2" } } run-async-2.3.0/test.js000066400000000000000000000072741302020202600147450ustar00rootroot00000000000000'use strict'; var assert = require('assert'); var runAsync = require('./index'); describe('runAsync', function () { it('run synchronous method', function (done) { var ranAsync = false; var aFunc = function () { return 'pass1'; }; runAsync(aFunc, function (err, val) { assert.ifError(err); assert(ranAsync); assert.equal(val, 'pass1'); done(); })(); ranAsync = true; }); it('run asynchronous method', function (done) { var aFunc = function () { var returns = this.async(); setImmediate(returns.bind(null, null, 'pass2')); }; runAsync(aFunc, function (err, val) { assert.ifError(err); assert.equal(val, 'pass2'); done(); })(); }); it('pass arguments', function (done) { var aFunc = function (a, b) { assert.equal(a, 1); assert.equal(b, 'bar'); return 'pass1'; }; runAsync(aFunc, function (err, val) { assert.ifError(err); done(); })(1, 'bar'); }); it('allow only callback once', function (done) { var aFunc = function () { var returns = this.async(); returns(); returns(); }; 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(); })(); }); it('returns a promise that is resolved', function (done) { var returns = function () { return 'hello'; }; runAsync(returns)().then(function (result) { assert.equal(result, 'hello'); done(); }); }); it('returns a promise that is rejected', function (done) { var throws = function () { throw new Error('sync error'); }; runAsync(throws)().catch(function (reason) { assert.equal(reason.message, 'sync error'); done(); }); }); }); describe('runAsync.cb', function () { it('handles callback parameter', function (done) { var fn = function (cb) { setImmediate(function () { cb(null, 'value'); }); }; runAsync.cb(fn, function (err, val) { assert.ifError(err); assert.equal('value', val); done(); })(); }); it('run synchronous method', function (done) { var ranAsync = false; var aFunc = function () { return 'pass1'; }; runAsync.cb(aFunc, function (err, val) { assert.ifError(err); assert(ranAsync); assert.equal(val, 'pass1'); done(); })(); ranAsync = true; }); it('handles a returned promise', function (done) { var aFunc = function (a) { return Promise.resolve('foo' + a); }; runAsync.cb(aFunc, function(err, result) { assert.ifError(err); assert.equal(result, 'foobar'); done(); })('bar'); }); });