nl_tokens: Run a single function promise return promise factory or any other combination - then a callback when finished Take a function reference and treat it as a callback style function If the function returns a promise this behaviour is transformed into a callback style function
pl_tokens: function run ( context , fn , finish , args ) { // Argument mangling {{{ if ( typeof context == 'function' ) { // called as run(fn, finish, args); args = finish ; finish = fn ; fn = context ; context = this ; } // }}} if ( isPromise ( fn ) ) { // Given a promise that has already resolved? fn . then ( function ( value ) { finish . apply ( context , [ null , value ] ) ; } ) . catch ( function ( err ) { finish . call ( context , err || 'An error occured' ) ; } ) ; } else if ( hasCallback ( fn ) ) { // Callback w/ (err, result) pattern var result = fn . apply ( context , args ? [ finish ] . concat ( args ) : [ finish ] ) ; if ( isPromise ( result ) ) { result . then ( function ( value ) { // Remap result from (val) => (err, val) finish . apply ( context , [ null , value ] ) ; } ) . catch ( function ( err ) { finish . call ( context , err || 'An error occured' ) ; } ) ; } } else { // Maybe either a promise or sync function? var result ; try { result = fn . apply ( context , args || [ ] ) ; // Run the function and see what it gives us } catch ( e ) { finish . call ( context , e ) ; } if ( isPromise ( result ) ) { // Got back a promise - attach to the .then() function result . then ( function ( value ) { // Remap result from (val) => (err, val) finish . apply ( context , [ null , value ] ) ; } ) . catch ( function ( err ) { finish . <mask> ( context , err || 'An error occured' ) ; } ) ; } else { // Didn't provide back a promise - assume it was a sync function finish . apply ( context , [ null , result ] ) ; } } }