【发布时间】:2015-07-03 17:59:39
【问题描述】:
我正在尝试用 Promise 来理解链接和错误处理。在这里,我有一些承诺。
return ad_fetcher.getAds(live_rail_url, ad_time, req.sessionID)
.spread(generator.playlist_manipulate) // returns Promise.resolve([data, anotherData])
.then(client.incrAsync(config.channel_name + ":ad_hits", "vdvd")) // FOCUS HERE
.then(function() {
console.log("AD FETCHED AND PLAYLIST GENERATED.");
res.send(generator.generate_regular(config.default_bitrate));
})
.catch(function(err) {
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
console.log("!!! AD FETCHER - THERE WAS AN ERROR:!!!!!!!!!!!");
client.sadd(config.channel_name + ":ad_errors", err);
client.incr(config.channel_name + ":ad_errors:count");
console.log(err);
console.log('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
res.send(generator.generate_regular(config.default_bitrate));
});
现在在client.incrAsync(config.channel_name + ":ad_hits", "vdvd") 行,我故意编写错误的语法以查看.catch 是否捕获到错误。但是当我运行它时,我得到了这个:
未处理的拒绝错误:ERR 'incr' 的参数数量错误 命令
但是当我改变这个承诺的用法时:
.
.
.then(function() {
return client.incrAsync(config.channel_name + ":ad_hits", "vdvd");
})
.
.
错误被很好地捕获。它不再是“未处理的”。
我不明白这种行为。 incrAsync 不返回一个承诺,所以它的错误应该被链末尾的.catch 捕获吗?
注意:我承诺了 redis 客户端,毫无疑问。
谢谢!
【问题讨论】:
-
见这里developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…。这是一个关于函数的问题,而不是关于 Promise 的问题。
-
这确实是一个关于promises @elclanrs 的问题。如果您想就详细信息向我们提供启发,请不要犹豫,添加内容丰富的答案。
-
试试
.then(client.incrAsync.bind(client, config.channel_name + ":ad_hits", "vdvd"))
标签: javascript node.js error-handling promise bluebird