[Pkg-javascript-commits] [node-run-async] 01/04: Import Upstream version 2.3.0

Paolo Greppi paolog-guest at moszumanska.debian.org
Thu Dec 22 23:38:08 UTC 2016


This is an automated email from the git hooks/post-receive script.

paolog-guest pushed a commit to branch master
in repository node-run-async.

commit fdd31b67039377bdf3e1b8b3b8b1c8d7939a33fc
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date:   Fri Dec 16 18:40:06 2016 +0000

    Import Upstream version 2.3.0
---
 .editorconfig  |  12 +++++
 .gitattributes |   1 +
 .gitignore     |   1 +
 .jshintrc      |  20 +++++++
 .travis.yml    |   7 +++
 LICENSE        |  21 ++++++++
 README.md      |  79 +++++++++++++++++++++++++++
 index.js       |  61 +++++++++++++++++++++
 package.json   |  29 ++++++++++
 test.js        | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 397 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..6d740d5
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,12 @@
+# 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
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..176a458
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+* text=auto
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c2658d7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules/
diff --git a/.jshintrc b/.jshintrc
new file mode 100644
index 0000000..3e4ba5a
--- /dev/null
+++ b/.jshintrc
@@ -0,0 +1,20 @@
+{
+  "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" ]
+}
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..de72e16
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,7 @@
+sudo: false
+language: node_js
+node_js:
+  - v0.12
+  - v4
+  - v5
+  - v6
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..e895e99
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8eb62c2
--- /dev/null
+++ b/README.md
@@ -0,0 +1,79 @@
+Run 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.
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..f623326
--- /dev/null
+++ b/index.js
@@ -0,0 +1,61 @@
+'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);
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..6210cfb
--- /dev/null
+++ b/package.json
@@ -0,0 +1,29 @@
+{
+  "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 <admin at simonboudrias.com>",
+  "license": "MIT",
+  "dependencies": {
+    "is-promise": "^2.1.0"
+  },
+  "devDependencies": {
+    "mocha": "^3.1.2"
+  }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..63a95a8
--- /dev/null
+++ b/test.js
@@ -0,0 +1,166 @@
+'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');
+  });
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-run-async.git



More information about the Pkg-javascript-commits mailing list