wrap-promise
Like new Promise()
, but prevents implicit rejection
Comparison
new Promise()
Using the native const fs = ; { // Node's fs.readFile throws a type error when the first argument is not a string. fs;};
Using wrap-promise
const fs = ;const wrapPromise = ; ;
According to the Promise specification, a promise
will be rejected implicitly when an error is thrown in the constructor callback. The only (and the biggest) difference is that wrap-promise immediately throws an error in such a case.
Installation
Package managers
npm
npm install wrap-promise
Bower
bower install wrap-promise
Duo
const wrapPromise = ;
Standalone
Download the script file directly.
API
wrapPromise(fn)
fn: Function
Return: Object
(Promise)
It can be used in the same way as new Promise()
but new
operator is not needed.
wrapPromise.Promise
Type: Function
Default: global Promise
or require('es6-promise').Promise
The Promise
constructor used in wrapPromise
function.
CommonJS-based environment (e.g. Node)
OnBy default it uses the global Promise
constructor if available, otherwise it require
s es6-promise and use its Promise
property.
If you don't need the fallback, use no-fallback.js
instead. (Useful for Browserify)
const wrapPromise = ;
On non-CommonJS environment
It uses the global Promise
constructor without any fallbacks. Before using wrapPromise
, you must load Promise
polyfill if Promise
doesn't exist by default.
License
Copyright (c) 2014 - 2015 Shinnosuke Watanabe
Licensed under the MIT License.