Bluebird的Promise.settle无法使用正确的值解析 [英] Bluebird's Promise.settle doesn't resolve with the correct values

查看:105
本文介绍了Bluebird的Promise.settle无法使用正确的值解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

return Promise.settle(matches, imgur.uploadUrl)
    .map(function (inspection) {
        if (inspection.isFulfilled()) {
            return inspection.value().data.link;
        }
        return '#';
    })

上述更详细的版本显示同样的问题:

A more verbose version of the above displays the same problems:

return Promise.settle(matches, function(match) { return imgur.uploadUrl(match); })
    .then(function(results) {
        return results;
    })
    .map(function (inspection) {
        if (inspection.isFulfilled()) {
            return inspection.value().data.link;
        }
        return '#';
    })

其中

  • Promise = bluebird's promise
  • matches = an array of image links extracted from a string
  • imgur = https://github.com/kaimallea/node-imgur

预期的行为是 .map 的结果是一个解决的承诺原始数组中的图像上传到imgur(或'#',以防上传因任何原因失败)后的一系列imgur链接。

The expected behavior is that the result of .map is a promise which resolves with an array of imgur links after the images in the original array was uploaded to imgur (or '#', in case the upload failed for any reason).

相反,Promise.settle立即解析 (即似乎不等待imgur上传),以及 inspection.value()是来自匹配数组的原始图像网址(在尝试读取<$ c时出错$ c> .data.link 字符串属性。)

What happens instead is that Promise.settle resolves instantly (i.e. doesn't seem to wait for the imgur uploads), and inspection.value() is the original image url from the matches array (which gives an error when trying to read the .data.link property of a string).

为什么会发生这种情况?为什么不上传到imgur并正确解析?

Why does this happen? Why won't it upload to imgur and resolve correctly?

推荐答案

当我看到 Bluebird来源 Promise.settle() ,我只看到它处理第一个参数(期望一组承诺)。当你想要完成所有的承诺时,我总是把它用作 Promise.all()的替代品,即使有些错误也是如此。

When I look at the Bluebird source for Promise.settle(), I only see that it processes the first argument (expecting an array of promises). I've always just used it as a substitute for Promise.all() when you want all promises to complete, even if some have errors.

我想知道 .settle()的Bluebird文档是否错误,它将一个函数作为处理第一个数组的第二个参数?代码有点难以理解,但我不知道 Promise.settle()是如何使用第二个参数的(除非这不是正确的代码我'我正在寻找某种原因)。

I wonder if the Bluebird documentation for .settle() is just wrong about it taking a function as the second argument that will process the first array? The code is a little hard to follow, but I don't see how Promise.settle() ever uses the 2nd argument (unless this isn't the right code I'm looking at for some reason).

正如您所指出的,另一种选择是:

As you pointed out, an alternative is:

Promise.settle(matches.map(imgur.uploadUrl)).then(...)

刚刚通过一系列承诺 .settle()

FYI ,我通过创建一个简单的测试用例并在调试器中进入 Promise.settle()进行验证,它从不使用传递给它的第二个参数。这似乎是文档与实现不匹配的情况。我希望有人计划实施所记录的内容,但从未完成该实现。

FYI, I verified by creating a simple test case and stepping into Promise.settle() in the debugger that it never uses the second argument passed to it. This appears to be a case of the documentation not matching the implementation. I expect someone planned to implement what is documented, but never completed that implementation.

这篇关于Bluebird的Promise.settle无法使用正确的值解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆