Skip to content

Commit

Permalink
Add experimental parley.callable() method (not sure whether we actual…
Browse files Browse the repository at this point in the history
…ly want this or not yet- it's generally better to use the machine runner)
  • Loading branch information
mikermcneil committed Mar 8, 2019
1 parent bf09a3a commit 307e6c0
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion lib/parley.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,48 @@ module.exports = function parley(handleExec, explicitCbMaybe, customMethods, tim
// Return deferred object
return π;

};
};//ƒ



/**
* parley.callable()
*
* Build a simple function which returns a Deferred object.
* > This is a shortcut for building simple functions when you don't need the
* > full customizability of calling parley() to build a Deferred for you on the
* > fly (e.g. b/c you don't care about support chain-ability)
*
* CURRENTLY EXPERIMENTAL!
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* @param {AsyncFunction} gracefullyHandleExec
* @param {Number?} timeout
* @param {Error?} omen
*
* @returns {Function}
*/
module.exports.callable = function(gracefullyHandleExec, timeout, omen){

console.warn('WARNING: parley.callable() is currently experimental and should not be relied upon.');

if (!_.isFunction(gracefullyHandleExec) || gracefullyHandleExec.constructor.name !== 'AsyncFunction') {
throw new Error('parley.callable() expects an async function (e.g. `async ()=>{}`) to be provided as the first argument. Instead, got: '+require('util').inspect(gracefullyHandleExec));
}//•

return function handleCalling(/*…*/) {
var parley = module.exports;
var returnedFnArguments = arguments;
var returnedFnCtx = this;//« should really never matter, we just do it this way for consistency
omen = omen || flaverr.omen(handleCalling);
return parley(function(done) {
gracefullyHandleExec.apply(returnedFnCtx, returnedFnArguments)
.then(function(result) {
done(undefined, result);
})
.catch(function(err) {
done(err);
});
}, undefined, undefined, timeout, omen, undefined);
};//ƒ

};//ƒ

0 comments on commit 307e6c0

Please sign in to comment.