defer-promise
v3.0.0
Published
Isomorphic function returning a deferred promise. Uses native `Promise.defer` if available, else polyfills.
Downloads
64,016
Readme
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 <[email protected]>. Documented by jsdoc-to-markdown.