defer-promise
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/defer-promise package

3.0.0 • Public • Published

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.

Package Sidebar

Install

npm i defer-promise

Weekly Downloads

6,425

Version

3.0.0

License

MIT

Unpacked Size

4.45 kB

Total Files

5

Last publish

Collaborators

  • 75lb