wait-for-stuff
Advanced tools
Comparing version 1.1.1 to 1.2.0
26
index.js
@@ -237,2 +237,28 @@ /////////////////////////////////////////////////////////// | ||
$wait.use('callback', (nodeAsyncFunction, ...args) => { | ||
var isDone = false; | ||
var result = null; | ||
nodeAsyncFunction(...args, (...resultSet) => { | ||
result = resultSet.filter(r => r !== null && r !== undefined); | ||
isDone = true; | ||
}); | ||
deasync.loopWhile(() => !isDone); | ||
return result.length > 1 ? result : result[0] | ||
}); | ||
$wait.use('function', (customAsyncFunction, ...args) => { | ||
var isDone = false; | ||
var result = null; | ||
customAsyncFunction(...args, (...resultSet) => { | ||
result = resultSet; | ||
isDone = true; | ||
}); | ||
deasync.loopWhile(() => !isDone); | ||
return result; | ||
}); | ||
// as a convenience, we add 'condition' as an alias to 'predicate' | ||
@@ -239,0 +265,0 @@ $wait.alias('predicate', 'condition'); |
{ | ||
"name": "wait-for-stuff", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "an extendable library that can wait for stuff to happen in a synchronous-but-not-blocking manner", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,5 +17,7 @@ [![Build Status](https://travis-ci.org/ujc/wait-for-stuff.svg?branch=master)](https://travis-ci.org/ujc/wait-for-stuff) | ||
* **generator** *(wait for `generator` to fully exhaust all values)* | ||
* **callback** *(wait for node-style `callback` to be called)* | ||
* **function** *(wait for custom callback `function` to be called)* | ||
* *(node-style callbacks coming soon)* | ||
* *(composable \ chainable \ follow-through waiters are coming soon)* | ||
@@ -225,3 +227,58 @@ --- | ||
**`wait.for.callback(nodeAsyncFunction, ...params)`** waits until the `nodeAsyncFunction` has finished, passing to it any `params` that you supply. | ||
**note:** with node-style callbacks, there's usually an `error` as the first argument, and any possible `data` argument comes after that. either of these might will be null if the other isn't, and there can be more than one `data` argument. `wait.for.callback` will make an attempt to simplify the result value when possible. | ||
**see also:** **`wait.for.function`** *(below)* | ||
```javascript | ||
// instead of this: | ||
// ----------------------------------------------- | ||
// fs.readFile('foo.json', function(err, data){ | ||
// do something with err or data | ||
// }); | ||
// ----------------------------------------------- | ||
var errOrData = wait.for.callback(fs.readFile, 'foo.json'); | ||
/////////////////////////////////////////////////////// | ||
// or, if unlike fs.readFile, the function may pass | ||
// more than just "err" or "data": | ||
// instead of this: | ||
// moreComplexFunc('foo.json', function(err, data1, data2, data3){ | ||
// do something with err, or data1 + data2 + data3 | ||
// }); | ||
var errOrResultSet = wait.for.callback(moreComplexFunc, 'foo.json'); | ||
// errOrResultSet will either be 'err', | ||
// or an array containing [data1, data2, data3] in order | ||
``` | ||
<br /><br /> | ||
**`wait.for.function(customAsyncFunction, ...params)`** waits until the `customAsyncFunction` has finished, passing to it any `params` that you supply. | ||
unlike `wait.for.callback`, any arguments that were passed into the callback will be returned as the complete `resultSet` of the `customAsyncFunction` | ||
```javascript | ||
// instead of this: | ||
// ----------------------------------------------- | ||
// fs.readFile('foo.json', function(err, data){ | ||
// do something with err or data | ||
// }); | ||
// ----------------------------------------------- | ||
var resultSet = wait.for.function(fs.readFile, 'foo.json'); | ||
// resultSet is an array of [err, data] in order | ||
``` | ||
<br /><br /> | ||
--- | ||
@@ -231,3 +288,3 @@ ## Middleware | ||
once you've built your own waiter-middleware, you can add it to `wait-for-stuff` using the **`wait.use(middleware)`** api. | ||
once you've built your own waiter-middleware, you can add it to `wait-for-stuff` using the **`wait.use(name, middleware)`** api. | ||
@@ -234,0 +291,0 @@ **`wait.use(name, middleware)`** adds `middleware` as an additional waiter to `wait-for-stuff` under **`wait.for.<name>`**. |
@@ -140,2 +140,16 @@ var EventEmitter = require('events').EventEmitter; | ||
}); | ||
it('waits-for: callback', () => { | ||
var result = wait.for.callback(fs.readFile, __filename); | ||
result.toString().should.include('waits-for: callback'); | ||
}); | ||
it('waits-for: function', () => { | ||
var resultSet = wait.for.function(fs.readFile, __filename); | ||
var error = resultSet[0]; | ||
var buffer = resultSet[1]; | ||
should.not.exist(error); | ||
buffer.toString().should.include('waits-for: function'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25091
321
335