bluebird 的最佳导入变量名称

问题描述 投票:0回答:1

在一些 NodeJs 项目中,开发人员将 bluebird 包导入为

const Promise = require('bluebird')

在某些项目中,它是使用不同的变量导入的

喜欢

const bluebird = require('bluebird')

我的问题是这里导入的变量名称的最佳命名约定是什么。已经有一个内置的 NodeJs Promise,那么它会与这个内置名称冲突吗? 请帮忙理解一下。

node.js npm bluebird
1个回答
0
投票

从包中导入某些内容将覆盖已声明的同名全局变量:

const Promise = require('bluebird');
console.log(Promise.all);
// logs
// ƒ (promises) {
//   return new PromiseArray(promises).promise();
// }

与:

console.log(Promise.all);
// logs
// ƒ all() { [native code] }

由于 bluebird 包具有与内置对象相同的 API,因此您可以安全地将其导入为

Promise
,而不缺乏兼容性。

© www.soinside.com 2019 - 2024. All rights reserved.