Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

28 Commits

Repository files navigation

universalify

GitHub Workflow Status (branch)Coveralls github branchnpmnpm

Make a callback- or promise-based function support both promises and callbacks.

Uses the native promise implementation.

Installation

npm install universalify

API

universalify.fromCallback(fn)

Takes a callback-based function to universalify, and returns the universalified function.

Function must take a callback as the last parameter that will be called with the signature (error, result). universalify does not support calling the callback with three or more arguments, and does not ensure that the callback is only called once.

functioncallbackFn(n,cb){setTimeout(()=>cb(null,n),15)}constfn=universalify.fromCallback(callbackFn)// Works with Promises:fn('Hello World!').then(result=>console.log(result))// -> Hello World!.catch(error=>console.error(error))// Works with Callbacks:fn('Hi!',(error,result)=>{if(error)returnconsole.error(error)console.log(result)// -> Hi!})

universalify.fromPromise(fn)

Takes a promise-based function to universalify, and returns the universalified function.

Function must return a valid JS promise. universalify does not ensure that a valid promise is returned.

functionpromiseFn(n){returnnewPromise(resolve=>{setTimeout(()=>resolve(n),15)})}constfn=universalify.fromPromise(promiseFn)// Works with Promises:fn('Hello World!').then(result=>console.log(result))// -> Hello World!.catch(error=>console.error(error))// Works with Callbacks:fn('Hi!',(error,result)=>{if(error)returnconsole.error(error)console.log(result)// -> Hi!})

License

MIT

About

Make a callback- or promise-based function support both promises and callbacks.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages