我可以通过 bluebird 在节点全局范围内的实现来覆盖 ES6 的 Promise 吗?
Can I override ES6's Promise by bluebird's implementation in node's global scope?
我想使用 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 页面告诉我:
var Promise = require("bluebird");
,这会导致覆盖本机 Promise 元素。因为 bluebird 是规范的超集,它不会破坏现有代码,因此应该可以安全使用。
但是,因为我知道以下行为被认为是不好的做法:
- 扩展或替换本地语言,并且
- 定义全局变量以在依赖于它的需求链中使用
,当我想将其包含在节点应用程序的基本脚本中时,我很谨慎:
import Promise from 'bluebird';
global.Promise = Promise;
这是一种不好的做法吗?我应该坚持在每个文件中导入 bluebird 吗?
在过去的 4 年里,我在我的代码中这样做了数百次,所以在每月 1000 万次下载中还有很多其他的。
官方支持与bluebird交换原生实现。
我愿意
const Promise = require("bluebird");
基于每个文件。请注意,通常您可以承诺您的 API 一次,然后通常避免调用 Promise
- 最多调用 .resolve
。
我想使用 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 页面告诉我:
var Promise = require("bluebird");
,这会导致覆盖本机 Promise 元素。因为 bluebird 是规范的超集,它不会破坏现有代码,因此应该可以安全使用。
但是,因为我知道以下行为被认为是不好的做法:
- 扩展或替换本地语言,并且
- 定义全局变量以在依赖于它的需求链中使用
,当我想将其包含在节点应用程序的基本脚本中时,我很谨慎:
import Promise from 'bluebird';
global.Promise = Promise;
这是一种不好的做法吗?我应该坚持在每个文件中导入 bluebird 吗?
在过去的 4 年里,我在我的代码中这样做了数百次,所以在每月 1000 万次下载中还有很多其他的。
官方支持与bluebird交换原生实现。
我愿意
const Promise = require("bluebird");
基于每个文件。请注意,通常您可以承诺您的 API 一次,然后通常避免调用 Promise
- 最多调用 .resolve
。