Skip to content

Isomorphic function returning a deferred promise. Uses native `Promise.defer` where available, else polyfills

License

Notifications You must be signed in to change notification settings

75lb/defer-promise

Repository files navigation

view on npm npm module downloads Gihub repo dependents Gihub package dependents Node.js CI js-standard-style

defer-promise

Isomorphic function returning a deferred promise with resolve and reject methods. If the global Promise.defer() method exists it will use that, else polyfill.

import defer from 'defer-promise'
const deferred = defer()

/* Async function using a callback instead of returning a promise */
doSomething((result, err) => {
  if (err) {
    deferred.reject(err)
  } else {
    deferred.resolve(result)
  }
})

const result = await deferred.promise;

Load anywhere

This library can be loaded anywhere, natively without transpilation.

Common JS:

const defer = require('defer-promise')

Node.js with ECMAScript Module support enabled:

import defer from 'defer-promise'

Modern browser ECMAScript Module:

import defer from './node_modules/defer-promise/index.js'

© 2015-22 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.

About

Isomorphic function returning a deferred promise. Uses native `Promise.defer` where available, else polyfills

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published