Comparing version 1.0.0 to 1.0.1
43
index.js
/** | ||
* Expose `callAsync`. | ||
* Expose `callasync`. | ||
*/ | ||
module.exports = callAsync; | ||
module.exports = callasync; | ||
/** | ||
* A queue holding functions to execute on timeout. | ||
* @type {Array} | ||
*/ | ||
var waiting = []; | ||
/** | ||
* Waiting queue timeout handle. | ||
* @type {number} | ||
*/ | ||
var waitingID = 0; | ||
var callWaiting = function () { | ||
var funcs = waiting; | ||
/** | ||
* Waiting queue handler function. | ||
* @type {Function} | ||
*/ | ||
waiting = []; | ||
waitingID = 0; | ||
var process = function () { | ||
var index = -1; | ||
var length = waiting.length; | ||
var funcs = waiting; // local scope copy of waiting queue | ||
while (funcs.length) { | ||
funcs.shift()(); | ||
waiting = []; // drop waiting queue | ||
waitingID = 0; // reset waiting handle | ||
while (++index < length) { | ||
funcs[index](); | ||
} | ||
}; | ||
function callAsync(func) { | ||
/** | ||
* Add `func` to the waiting queue to execute it on the next timeout. | ||
* @param {Function} func The function to delay. | ||
*/ | ||
function callasync(func) { | ||
waiting.push(func); | ||
if (waitingID === 0) { | ||
waitingID = setTimeout(callWaiting, 0); | ||
waitingID = setTimeout(process, 0); | ||
} | ||
} |
{ | ||
"name": "callasync", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Group and call functions asynchronously", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test.js | tap-spec" | ||
"test": "tape test.js | tap-spec", | ||
"test-cov": "nyc npm test", | ||
"report-cov": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
"keywords": ["async"], | ||
"repository": { | ||
@@ -20,2 +23,4 @@ "type": "git", | ||
"devDependencies": { | ||
"coveralls": "^2.11.16", | ||
"nyc": "^10.1.2", | ||
"tap-spec": "^4.1.1", | ||
@@ -22,0 +27,0 @@ "tape": "^4.6.3" |
@@ -1,10 +0,37 @@ | ||
# Call Async | ||
# callasync | ||
Essential function to group all your asynchronous function calls into | ||
a single queue. | ||
[![npm version](https://badge.fury.io/js/callasync.svg)](https://badge.fury.io/js/callasync) | ||
[![Build Status](https://travis-ci.org/yefremov/callasync.svg?branch=master)](https://travis-ci.org/yefremov/callasync) | ||
[![Coverage Status](https://coveralls.io/repos/github/yefremov/callasync/badge.svg?branch=master)](https://coveralls.io/github/yefremov/callasync?branch=master) | ||
Essential utility function that groups and calls functions asynchronously on | ||
the same tick respecting order of invocation. | ||
## Installation | ||
```bash | ||
$ npm install --save callasync | ||
``` | ||
## API | ||
```js | ||
var callasync = require('callasync'); | ||
// both functions `foo()` and `bar()` will be | ||
// called async but on a same tick | ||
callasync(function foo() { | ||
// function body | ||
}); | ||
callasync(function bar() { | ||
// function body | ||
}); | ||
``` | ||
## Running tests | ||
``` | ||
```bash | ||
$ npm install | ||
@@ -16,2 +43,2 @@ $ npm test | ||
[MIT](LICENSE) | ||
[MIT](LICENSE) |
15
test.js
var test = require('tape'); | ||
var callAsync = require('.'); | ||
var callasync = require('.'); | ||
test('callAsync should call `func` asynchronously', function (t) { | ||
test('callasync(func) should call `func` asynchronously', function (t) { | ||
t.plan(4); | ||
var i = 0; | ||
@@ -12,11 +14,6 @@ var callback = function (n, msg) { | ||
t.plan(4); | ||
callback(1, 'should execute first')(); | ||
callAsync(callback(3, 'should execute third')); | ||
callAsync(callback(4, 'should execute fourth')); | ||
callasync(callback(3, 'should execute third')); | ||
callasync(callback(4, 'should execute fourth')); | ||
callback(2, 'should execute second')(); | ||
}); |
4237
8
53
44
4