asyncjs-util
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -8,3 +8,3 @@ { | ||
"devDependencies": { | ||
"tap": "14.1.7" | ||
"tap": "14.2.2" | ||
}, | ||
@@ -25,3 +25,3 @@ "keywords": [ | ||
}, | ||
"version": "1.0.0" | ||
"version": "1.1.0" | ||
} |
/** Return a specific result of an async/auto process | ||
Omit a cbk and specify reject/resolve if using a Promise | ||
{ | ||
[of]: <Property String> | ||
[reject]: <Promise Reject Function> | ||
[resolve]: <Promise Resolve Function> | ||
} | ||
@@ -11,13 +15,17 @@ | ||
module.exports = (args, cbk) => { | ||
if (!cbk && !(!!args.reject && !!args.resolve)) { | ||
throw new Error('ExpectedCbkOrPromiseFunctionsToReturnResult'); | ||
} | ||
return (err, res) => { | ||
if (!!err) { | ||
return cbk(err); | ||
return !cbk ? args.reject(err) : cbk(err); | ||
} | ||
if (!!args.of) { | ||
return cbk(null, res[args.of]); | ||
return !cbk ? args.resolve(res[args.of]) : cbk(null, res[args.of]); | ||
} | ||
return cbk(); | ||
return !cbk ? args.resolve() : cbk(); | ||
}; | ||
}; |
@@ -27,7 +27,28 @@ const {test} = require('tap'); | ||
test('A callback or promise function is required', ({end, throws}) => { | ||
throws( | ||
() => returnResult({}), | ||
new Error('ExpectedCbkOrPromiseFunctionsToReturnResult') | ||
); | ||
return end(); | ||
}); | ||
tests.forEach(({args, description, error, expected, result}) => { | ||
return test(description, ({deepEqual, end, equal}) => { | ||
const promise = (err, resolution) => new Promise((resolve, reject) => { | ||
return returnResult({reject, resolve, of: args.of})(err, resolution); | ||
}); | ||
return test(description, async ({deepEqual, end, equal, rejects}) => { | ||
// Promise methods | ||
if (!error) { | ||
equal(await promise(null, {foo: result.res}), expected); | ||
} else { | ||
rejects(promise(error), result.err); | ||
} | ||
// Callback methods | ||
return returnResult(args, (err, res) => { | ||
deepEqual(err, error); | ||
equal(res, expected); | ||
deepEqual(err, error, 'Callback returns error'); | ||
equal(res, expected, 'Callback returns result'); | ||
@@ -34,0 +55,0 @@ return end(); |
Sorry, the diff of this file is not supported yet
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
3924
77