- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个大致以下的代码(元CoffeeScript):
xml = new ...
Promise.promisifyAll xml
allRecords = []
xml.on 'frequentTag', (item) ->
...
iPromise = /* some promise made from item */
allRecords.push iPromise
xml.onAsync('end').then -> Promise.settle allRecords
现在的问题是:我可以摆脱 allRecords
累加器吗?
最佳答案
promise 代表一个单一值。这是一个值。
没有简单的方法来 promise 事件发射器,因为它不提供像单个值的 Node 样式回调这样的标准契约 - 因为不同的事件被多次调用。
在 EventEmitter
上执行 Promise.promisifyAll
本身不会产生有意义的结果,您必须手动 Promisify。好消息是,如果您碰巧经常这样做,您可以自己将其提取到函数中,或者使用 Bluebird 的 promisifyAll
和 promisifier 参数并让它为您执行遍历。
Bluebird 不了解您的事件发射器及其定义的有关何时调用回调以及如何调用的合约。假设它以 end
事件结束,带有 error
事件的错误数据会随着 frequentTag
事件流入 - 我会这样做:
xmlProto.prototype.getTags = function getTags(){
// if you do this very often, this can be faster
return new Promise(function(resolve, reject){
var results = [];
this.on("error", reject);
this.on("frequentTag", function(item){
iPhomise = /* some promise made from item */
results.push(iPromise);
});
this.on("end", resolve.bind(null, results));
}.bind(this)).settle(); // settle all
};
其中 xmlProto 是您新的
的内容,这可以让您执行以下操作:
xml = new ...
xml.getTags().then(function(records){
// work with records
});
这可以提取到此类事件发射器的通用函数中 - 但您已经了解了总体思路。
关于node.js - Bluebird 的适当 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26090579/
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
我是一名优秀的程序员,十分优秀!