callbackpromise
v0.1.2
Published
Wrap a callback so that it's result is captured as a promise.
Downloads
4
Readme
callbackpromise
Wrap a callback so that the result is captured as a promise.
Usage
Returns a wrapper function that when executed will execute the original function and capture the result. The captured result is exposed as a Promise. If the callback returns a value then the promise resolves with that value. If the callback throws a value then the promise rejects with that value. Will return the captured result or error.
Example
function callback( error, value ) { if ( error !== undefined ) { throw error; } return value; }
let cbFunc1 = callbackPromise( callback ); cbFunc1( undefined, true ); console.log( cbFunc1.promise );
Promise { true }
let cbFunc2 = callbackPromise( callback ); cbFunc2( false, undefined ); console.log( cbFunc2.promise );
Promise { false }
License
ISC