- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关闭。这个问题是opinion-based .它目前不接受答案。
想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题.
5年前关闭。
Improve this question
在 Node.js 添加对 promise 的原生支持后,还有理由使用 Q 或 BlueBird 之类的库吗?
例如,如果您正在启动一个新项目,并且假设在该项目中您没有任何使用这些库的依赖项,我们是否可以说真的没有更多理由使用这些库?
最佳答案
古老的格言说,您应该为工作选择合适的工具。 ES6 promise 提供了基础。如果您想要或需要的只是基础知识,那么这应该/可以为您工作。但是,工具箱中的工具不仅仅是基础工具,而且在某些情况下,这些附加工具非常有用。而且,我认为 ES6 的 promises 甚至缺少一些基本的东西,比如在几乎每个 node.js 项目中都有用的 promisification。
我最熟悉的是Bluebird promise library所以我将主要根据我在那个图书馆的经验来发言。
所以,这里是我使用功能更强大的 Promise 库的 6 大理由
.promisify()
和 .promisifyAll()
对于处理所有那些仍然需要普通回调并且尚未返回 promise 的异步接口(interface)非常有用 - 一行代码创建了整个接口(interface)的 promise 版本。 Promise.mapSeries()
或 Promise.reduce()
允许您遍历数组,对每个元素调用异步操作,但对异步操作进行排序,以便它们一个接一个地发生,而不是同时发生。您可以这样做,因为目标服务器需要它,或者因为您需要将一个结果传递给下一个。 Promise.map()
, Promise.some()
, Promise.any()
, Promise.filter()
, Promise.each()
和 Promise.props()
所有这些都是偶尔得心应手的。虽然这些操作可以使用 ES6 promise 和附加代码执行,但 Bluebird 附带这些操作已经预先构建和预先测试,因此使用它们更简单,代码更少。 .then()
中创建一个新的 promise 。处理程序而不返回该 promise (将其链接到当前的 promise 链中),那么在大多数情况下,这是一个意外错误,Bluebird 会就此向您发出警告。其他内置的 Bluebird 警告是 described here . .promisifyAll()
很多标准的 node.js 模块,比如
fs
模块。
fs
那样为执行异步 IO 的内置模块提供 promise 接口(interface)。模块。因此,如果您想在这些接口(interface)中使用 Promise,您要么为您使用的每个模块函数手动编写一个 Promise 包装器,要么获取一个可以为您执行此操作的库,或者不使用 Promise。
Promise.promisify()
和
Promise.promisifyAll()
提供 node.js 调用约定异步 API 的自动包装以返回 promise 。这是非常有用和节省时间的。我一直在使用它。
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
fs.readFileAsync('somefile.text').then(function(data) {
// do something with data here
});
fs
手动创建自己的 promise 包装器。您想使用的 API:
const fs = require('fs');
function readFileAsync(file, options) {
return new Promise(function(resolve, reject) {
fs.readFile(file, options, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
readFileAsync('somefile.text').then(function(data) {
// do something with data here
});
Promise.promisify()
和
Promise.promisifyAll()
是这样的实用程序。
Promise.promisify()
Promise.promisifyAll()
Promise.map()
Promise.reduce()
Promise.mapSeries()
Promise.delay()
Promise.map()
还支持并发选项,可让您指定应允许同时运行多少个操作,这在您有很多事情要做但不能压倒某些外部资源时特别有用。
const fs = requirep('fs');
fs.readFileAsync('somefile.text').then(function(data) {
// do something with data here
});
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
fs.readFileAsync('somefile.text').then(function(data) {
// do something with data here
});
Promise.map()
的例子。用于并行读取一组文件并在完成所有数据后通知:
const files = ["file1.txt", "fileA.txt", "fileB.txt"];
const fs = require('fs');
// make promise version of fs.readFile()
function fsReadFileP(file, options) {
return new Promise(function(resolve, reject) {
fs.readFile(file, options, function(err, data) {
if (err) return reject(err);
resolve(data);
});
});
}
Promise.all(files.map(fsReadFileP)).then(function(results) {
// files data in results Array
}, function(err) {
// error here
});
Promise.map()
和 Promise.promisifyAll()
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs'));
const files = ["file1.txt", "fileA.txt", "fileB.txt"];
Promise.map(files, fs.readFileAsync).then(function(results) {
// files data in results Array
}, function(err) {
// error here
});
Promise.map()
的例子。从远程主机读取一堆 URL 时,您一次最多可以读取 4 个,但希望在允许的情况下保持尽可能多的并行请求:
const request = require('request');
const urls = [url1, url2, url3, url4, url5, ....];
// make promisified version of request.get()
function requestGetP(url) {
return new Promise(function(resolve, reject) {
request.get(url, function(err, data) {
if (err) return reject(err);
resolve(data);
});
});
}
function getURLs(urlArray, concurrentLimit) {
var numInFlight = 0;
var index = 0;
var results = new Array(urlArray.length);
return new Promise(function(resolve, reject) {
function next() {
// load more until concurrentLimit is reached or until we got to the last one
while (numInFlight < concurrentLimit && index < urlArray.length) {
(function(i) {
requestGetP(urlArray[index++]).then(function(data) {
--numInFlight;
results[i] = data;
next();
}, function(err) {
reject(err);
});
++numInFlight;
})(index);
}
// since we always call next() upon completion of a request, we can test here
// to see if there was nothing left to do or finish
if (numInFlight === 0 && index === urlArray.length) {
resolve(results);
}
}
next();
});
}
const Promise = require('bluebird');
const request = Promise.promisifyAll(require('request'));
const urls = [url1, url2, url3, url4, url5, ....];
Promise.map(urls, request.getAsync, {concurrency: 4}).then(function(results) {
// urls fetched in order in results Array
}, function(err) {
// error here
});
关于javascript - 既然我们有 ES6 promise ,还有理由使用 Q 或 BlueBird 之类的 promise 库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34960886/
bluebird.js和bluebird.core.js有什么区别? 什么时候应该使用bluebird.core.js而不是bluebird.js? 我无法在bluebird site或其他地方找到任
我正在从异步转换为 Bluebird,但不知道如何打破循环 这是我想要实现的目标: 循环数据数组。 对于每个项目,检查它是否存在于数据库中。 将一个项添加到数据库(第一个不存在的项),然后退出.eac
我试图用 .try(function(){}).catch(function(){}) 块返回一个 promise 。我的问题是由我的 promise 类型引起的。 deleteProcess
我正在尝试使用 bluebird 的 .return() 来扩展 promise 解析值方法。 目前我正在使用以下代码: doSomethingAsync() // assu
我刚刚开始使用 Promise 和 Bluebird。调试时我可以看到我的函数执行了两次: 首先我收到此错误:TypeError:未捕获错误:无法读取未定义的属性“then” 然后我看到函数再次执行,
我想测试数组的每个元素,直到满足条件,然后跳过其余的。这是我想出的代码,它似乎有效,但我不确定它是否真的安全或有意想不到的副作用。欢迎其他解决方案。 let buddyAdded = false; r
假设我有以下 node.js 代码 function foo() { return promiseOne().then(function(result) { return pr
假设我想在从数据库查找用户后同时发送电子邮件并向客户端推送通知,我可以这样写 User.findById(userId).exec() .then(() => sendMail()) .then(()
.call 方法的 Bluebird 文档有 code sample标记为“链接破折号或下划线方法”。 下面的代码片段中链接的 .then(_) 的用途是什么? var Promise = requi
是否有某种方法可以检索从上一个 then 回调返回的任何内容(或传递给初始 Promise.resolve()/resolve())? const p = Bluebird.resolve().the
我有以下代码。当 f2 没有抛出错误时,它工作正常。 如果有错误,它会生成一个Unhandled rejection Error。 重写代码以避免 Unhandled rejection Error
我有一个来自这篇文章的后续问题:Chaining Requests using BlueBird/ Request-Promise 我对 promise 很陌生,所以请原谅我的天真。我成功地实现了这段
我是 Bluebird 的新手,我正在尝试创建一个新用户,但 reject 函数没有按预期工作。 问题是它为我创建了用户,即使它启动了错误There nickname is already in us
我正在尝试实现剪刀石头布游戏的 CLI 版本。我正在使用查询器模块来处理 IO。我的主要功能如下所示: RockPaperScissors.prototype.gameLoop = function(
此代码运行正常: let promise; try { promise = parent(); //but I want: await parent(); await cont
在 promise 了 fs-extra 后,我知道我可以使用 then 来访问文件。我猜想有某种机制,在获取文件后,它知道要移动到 then 链中的下一个链接。然而,接下来的then我只是放置了一个
我有以下代码。它按预期工作,没有抛出未处理的拒绝错误。 p = new Promise (fulfill, reject) -> reject new Error 'some error' p.c
我期待 Bluebird forgotten return warning出现,但由于某种原因它不起作用。 A demo : const Bluebird = require('bluebird');
我正在使用 bluebird图书馆结束memcached . memcached.set('foo', 'bar', 10, function (err) { /* stuff */ }); 此函数不
我正在尝试如下使用 Bluebird 的协程: var p = require('bluebird'); //this should return a promise resolved to valu
我是一名优秀的程序员,十分优秀!