I want to use bluebird's implementation of the Promise/A+ open standard and override native ES6 Promises. I also want the bluebird implementation to be available everywhere in my subsequently imported modules without having to require it in every single one of them. Bluebird's Getting started page tells me to:
var Promise = require("bluebird");
, which results in overriding the native Promise element. Because bluebird is a superset of the spec, it will not break existing code and is thus supposed to be safe to use.
However, because I know it's considered bad practice to:
- extend or replace language natives, and
- define globals for use in a require chain that depends on it
, I'm wary when I want to include this in the base script of a node app:
import Promise from 'bluebird';
global.Promise = Promise;
Is this a bad practice? Should I stick to importing bluebird in every single file?
I want to use bluebird's implementation of the Promise/A+ open standard and override native ES6 Promises. I also want the bluebird implementation to be available everywhere in my subsequently imported modules without having to require it in every single one of them. Bluebird's Getting started page tells me to:
var Promise = require("bluebird");
, which results in overriding the native Promise element. Because bluebird is a superset of the spec, it will not break existing code and is thus supposed to be safe to use.
However, because I know it's considered bad practice to:
- extend or replace language natives, and
- define globals for use in a require chain that depends on it
, I'm wary when I want to include this in the base script of a node app:
import Promise from 'bluebird';
global.Promise = Promise;
Is this a bad practice? Should I stick to importing bluebird in every single file?
Share Improve this question edited Jun 8, 2016 at 19:03 Roamer-1888 19.3k5 gold badges34 silver badges45 bronze badges asked Jun 8, 2016 at 17:09 Christophe MaroisChristophe Marois 6,7191 gold badge32 silver badges34 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 19I have done this hundreds of times in my code over the last 4 years and so have plenty others among the 10 million monthly downloads.
It is officially supported to swap the native implementation with bluebird.
I do
const Promise = require("bluebird");
On a per-file basis. Note that usuaslly you can promisify your APIs once and then generally avoid calling Promise
- calling at most .resolve
.
require
d modules should also use Bluebird promises instead of native ones. – gcampbell Commented Jun 8, 2016 at 17:25