2.0.1 • Published 1 year ago

universalify v2.0.1

Weekly downloads
29,504,003
License
MIT
Repository
github
Last release
1 year ago

universalify

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 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.

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

const fn = 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) return console.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.

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

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

License

MIT

fs-extrajsonfileweb-desktop-helpercuc-gafarchetype-librarycomponennentteasy-select-rn@saucesteals/tough-cookievuedragdropuploadimagesreact-native-bluetooth2killi8n-react-native-fast-imagern-send-smsreact-native-template-rfbasecloud-archive-s3airscanairscan-examplebb-chatyijicolorbarreact-native-esc-pos-sahaab@ihikmawan/tailwindcss-multi-theme-l2@borisovart/atol-kkt-module@frxf/frxfdeneme323112@steven-torres/jsxr@fundefund/funde_ck@ntt_app/react-native-custom-notificationreact-native-custom-text-hwjamesreact-native-covid-sdkmina-sysgql_din_modbitgetalex-frontend-sdk-stagingafryxiconsreact-native-thanh-toast-library@jttechnic/interpreter@thanhnguyen14797/react-native-thanh-toast-library@eginnovations/eginnovations-capacitor-plugin@iobroker-community-adapters/iobroker.device-watcher@olivervorasai/sliderreact-native-printer-brothersrn-pdf-reader-offlineevery-func@newhorizon-tech/dd-npm-package-templatereact-native-shekhar-bridge-testcogoportutilsukor-remasteruncoded-connectdyx-reactlevibestliblevibestlib2levilibtest19levilibtest24levilibtest25levilibtest26levilibtest27levilibtest28levilibtest29wilscanner@oiti/documentoscopy-react-nativestretch-rollup@mink-opn/build-tokensquoc-testreact-native-slider-kfone-indexsvelte-slimecloudbasepaycclibyarntest@saaspe/componentsplginexpand-react-bridgeopea-bootstraapluminos-ui-coresklif-ui-kitsklif-api@everything-registry/sub-chunk-3015jawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryeginnovations-capacitor-pluginreact-native-credit-card-pkgp149-table@rabailriaz/hisaab-web-portalsklif-uireact-native-jawwy_samplelapture-ui-complaptureuigriffin-ui-library@wacoco/y@jfiles/coretaman-baca-masyarakattctstailwind-vector-effectsyncbackbasestailwind-templatetemplewallet-eslint-plugintemplet-cli-plussvelte-electronsvelvet-custom
2.0.1

1 year ago

2.0.0

4 years ago

1.0.0

5 years ago

0.2.0

5 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

8 years ago

0.0.1

8 years ago