Skip to content

Commit

Permalink
Add runIf() shortcut to run async function when it is provided
Browse files Browse the repository at this point in the history
PR-URL: #421
  • Loading branch information
belochub authored and nechaido committed Apr 11, 2019
1 parent f752f0d commit f3dd5c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,19 @@ const runIf = (condition, defaultVal, asyncFn, ...args) => {
}
};

// Run `asyncFn` if it is provided
// Signature: asyncFn, ...args
// asyncFn - <Function>, callback-last function that will be executed if it
// is provided
// args - <any[]>, args to pass to `asyncFn`
const runIfFn = (asyncFn, ...args) => {
runIf(asyncFn, undefined, asyncFn, ...args);
};

module.exports = {
firstOf,
parallel,
sequential,
runIf,
runIfFn,
};
18 changes: 18 additions & 0 deletions test/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,21 @@ metatests.test('runIf forward an error', test => {
test.end();
});
});

metatests.test('runIfFn', test => {
test.plan(5);
const value = 42;
const asyncFn = cb => {
cb(null, value);
};

metasync.runIfFn(test.mustCall(asyncFn), (err, res) => {
test.error(err);
test.strictSame(res, value);
});

metasync.runIfFn(null, (err, res) => {
test.error(err);
test.assertNot(res);
});
});

0 comments on commit f3dd5c9

Please sign in to comment.