@f1stnpm2/perspiciatis-aut-accusamus

1.0.0 • Public • Published

@f1stnpm2/perspiciatis-aut-accusamus

GitHub Workflow Status (branch) Coveralls github branch npm npm

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

Uses the native promise implementation.

Installation

npm install @f1stnpm2/perspiciatis-aut-accusamus

API

@f1stnpm2/perspiciatis-aut-accusamus.fromCallback(fn)

Takes a callback-based function to @f1stnpm2/perspiciatis-aut-accusamus, and returns the universalified function.

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

function callbackFn (n, cb) {
  setTimeout(() => cb(null, n), 15)
}

const fn = @f1stnpm2/perspiciatis-aut-accusamus.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) return console.error(error)
  console.log(result)
  // -> Hi!
})

@f1stnpm2/perspiciatis-aut-accusamus.fromPromise(fn)

Takes a promise-based function to @f1stnpm2/perspiciatis-aut-accusamus, and returns the universalified function.

Function must return a valid JS promise. @f1stnpm2/perspiciatis-aut-accusamus does not ensure that a valid promise is returned.

function promiseFn (n) {
  return new Promise(resolve => {
    setTimeout(() => resolve(n), 15)
  })
}

const fn = @f1stnpm2/perspiciatis-aut-accusamus.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) return console.error(error)
  console.log(result)
  // -> Hi!
})

License

MIT

/@f1stnpm2/perspiciatis-aut-accusamus/

    Package Sidebar

    Install

    npm i @f1stnpm2/perspiciatis-aut-accusamus

    Weekly Downloads

    3

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    19.1 kB

    Total Files

    10

    Last publish

    Collaborators

    • sinhatminh11