summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Early <alexander.early@gmail.com>2018-09-02 20:11:35 -0700
committerAlexander Early <alexander.early@gmail.com>2018-09-02 20:11:35 -0700
commit01292f5e5f1170fe8948f88b60e488c2274e021c (patch)
treef1a1ce4132cc9a8107022df20a0b01868e6a23b6
parent39dd9054bcad5864a01609dd77ac78b2aa7bfc33 (diff)
downloadasync-01292f5e5f1170fe8948f88b60e488c2274e021c.tar.gz
awaitable whilst/until (lol)
-rw-r--r--lib/doUntil.js3
-rw-r--r--lib/doWhilst.js11
-rw-r--r--lib/until.js3
-rw-r--r--lib/whilst.js6
-rw-r--r--test/es2017/awaitableFunctions.js68
5 files changed, 82 insertions, 9 deletions
diff --git a/lib/doUntil.js b/lib/doUntil.js
index d2e5b04..a484df6 100644
--- a/lib/doUntil.js
+++ b/lib/doUntil.js
@@ -20,10 +20,11 @@ import wrapAsync from './internal/wrapAsync';
* function has passed and repeated execution of `iteratee` has stopped. `callback`
* will be passed an error and any arguments passed to the final `iteratee`'s
* callback. Invoked with (err, [results]);
+ * @returns {Promise} a promise, if no callback is passed
*/
export default function doUntil(iteratee, test, callback) {
const _test = wrapAsync(test)
- doWhilst(iteratee, (...args) => {
+ return doWhilst(iteratee, (...args) => {
const cb = args.pop()
_test(...args, (err, truth) => cb (err, !truth))
}, callback);
diff --git a/lib/doWhilst.js b/lib/doWhilst.js
index b3d52a5..3d9a1af 100644
--- a/lib/doWhilst.js
+++ b/lib/doWhilst.js
@@ -1,7 +1,6 @@
-import noop from './internal/noop';
-
import onlyOnce from './internal/onlyOnce';
import wrapAsync from './internal/wrapAsync';
+import awaitify from './internal/awaitify'
/**
* The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
@@ -24,10 +23,10 @@ import wrapAsync from './internal/wrapAsync';
* function has failed and repeated execution of `iteratee` has stopped.
* `callback` will be passed an error and any arguments passed to the final
* `iteratee`'s callback. Invoked with (err, [results]);
- * @return undefined
+ * @returns {Promise} a promise, if no callback is passed
*/
-export default function doWhilst(iteratee, test, callback) {
- callback = onlyOnce(callback || noop);
+function doWhilst(iteratee, test, callback) {
+ callback = onlyOnce(callback);
var _fn = wrapAsync(iteratee);
var _test = wrapAsync(test);
var results
@@ -48,3 +47,5 @@ export default function doWhilst(iteratee, test, callback) {
return check(null, true);
}
+
+export default awaitify(doWhilst, 3)
diff --git a/lib/until.js b/lib/until.js
index 37d8922..0c407a3 100644
--- a/lib/until.js
+++ b/lib/until.js
@@ -22,6 +22,7 @@ import wrapAsync from './internal/wrapAsync';
* function has passed and repeated execution of `iteratee` has stopped. `callback`
* will be passed an error and any arguments passed to the final `iteratee`'s
* callback. Invoked with (err, [results]);
+ * @returns {Promise} a promise, if a callback is not passed
*
* @example
* const results = []
@@ -39,5 +40,5 @@ import wrapAsync from './internal/wrapAsync';
*/
export default function until(test, iteratee, callback) {
const _test = wrapAsync(test)
- whilst((cb) => _test((err, truth) => cb (err, !truth)), iteratee, callback);
+ return whilst((cb) => _test((err, truth) => cb (err, !truth)), iteratee, callback);
}
diff --git a/lib/whilst.js b/lib/whilst.js
index d42e6a7..35c9203 100644
--- a/lib/whilst.js
+++ b/lib/whilst.js
@@ -2,6 +2,7 @@ import noop from './internal/noop';
import onlyOnce from './internal/onlyOnce';
import wrapAsync from './internal/wrapAsync';
+import awaitify from './internal/awaitify';
/**
* Repeatedly call `iteratee`, while `test` returns `true`. Calls `callback` when
@@ -20,7 +21,7 @@ import wrapAsync from './internal/wrapAsync';
* function has failed and repeated execution of `iteratee` has stopped. `callback`
* will be passed an error and any arguments passed to the final `iteratee`'s
* callback. Invoked with (err, [results]);
- * @returns undefined
+ * @returns {Promise} a promise, if no callback is passed
* @example
*
* var count = 0;
@@ -37,7 +38,7 @@ import wrapAsync from './internal/wrapAsync';
* }
* );
*/
-export default function whilst(test, iteratee, callback) {
+function whilst(test, iteratee, callback) {
callback = onlyOnce(callback || noop);
var _fn = wrapAsync(iteratee);
var _test = wrapAsync(test);
@@ -59,3 +60,4 @@ export default function whilst(test, iteratee, callback) {
return _test(check);
}
+ export default awaitify(whilst, 3)
diff --git a/test/es2017/awaitableFunctions.js b/test/es2017/awaitableFunctions.js
index d118b42..25bdbc8 100644
--- a/test/es2017/awaitableFunctions.js
+++ b/test/es2017/awaitableFunctions.js
@@ -375,4 +375,72 @@ module.exports = function () {
expect(result).to.eql(4)
});
+ it('should return a Promise: whilst', async () => {
+ expect (async.whilst.name).to.contain('whilst')
+ const calls = []
+ let counter = 0
+ await async.whilst(
+ async () => {calls.push('test', counter); return counter < 5},
+ async () => { calls.push('fn'); counter++ }
+ );
+ expect(calls).to.eql([
+ 'test', 0, 'fn',
+ 'test', 1, 'fn',
+ 'test', 2, 'fn',
+ 'test', 3, 'fn',
+ 'test', 4, 'fn',
+ 'test', 5
+ ])
+ });
+ it('should return a Promise: until', async () => {
+ expect (async.until.name).to.contain('until')
+ const calls = []
+ let counter = 0
+ await async.until(
+ async () => {calls.push('test', counter); return counter === 5},
+ async () => { calls.push('fn'); counter++ }
+ );
+ expect(calls).to.eql([
+ 'test', 0, 'fn',
+ 'test', 1, 'fn',
+ 'test', 2, 'fn',
+ 'test', 3, 'fn',
+ 'test', 4, 'fn',
+ 'test', 5
+ ])
+ });
+ it('should return a Promise: doWhilst', async () => {
+ expect (async.doWhilst.name).to.contain('doWhilst')
+ const calls = []
+ let counter = 0
+ await async.doWhilst(
+ async () => { calls.push('fn'); counter++ },
+ async () => {calls.push('test', counter); return counter < 5}
+ );
+ expect(calls).to.eql([
+ 'fn',
+ 'test', 1, 'fn',
+ 'test', 2, 'fn',
+ 'test', 3, 'fn',
+ 'test', 4, 'fn',
+ 'test', 5
+ ])
+ });
+ it('should return a Promise: doUntil', async () => {
+ expect (async.doUntil.name).to.contain('doUntil')
+ const calls = []
+ let counter = 0
+ await async.doUntil(
+ async () => { calls.push('fn'); counter++ },
+ async () => {calls.push('test', counter); return counter === 5}
+ );
+ expect(calls).to.eql([
+ 'fn',
+ 'test', 1, 'fn',
+ 'test', 2, 'fn',
+ 'test', 3, 'fn',
+ 'test', 4, 'fn',
+ 'test', 5
+ ])
+ });
};