Node 中的 Promise.all 未定义 [英] Promise.all in Node is undefined

查看:26
本文介绍了Node 中的 Promise.all 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Node.js 中的 Promise 并尝试使用 Promise.all.我正在推送三个将承诺返回到数组中的函数,然后调用 Promise.all 但永远不会命中所有的解决方案.在调试器上,它还说 Promise.all 是未定义的".

I'm playing around with promises in Node.js and am trying to use Promise.all. I'm pushing three functions that return a promise into an array and then calling Promise.all but the all's resolve is never hit. On the debugger, it also says that Promise.all is "undefined".

为什么 Promise.all 永远不会返回,为什么它显示为未定义"?

Why is Promise.all never returning and why does it show up as "undefined"?

相关代码部分:

var updates = [];
updates.push(data.updateItemCondition(characterID, specificItemID_toUse, newCondition));
updates.push(data.updateCharacterLoot(characterID, newValue));
updates.push(data.updateSharedLoot(lootUpdateChange));


Promise.all(updates).then(function (success) {
    resolve("Item successfully used");
}, function (error) {
    resolve("Failed to use item " + error.toString());
});

所有三个函数如下所示,使用调试器我可以看到所有三个解析都被命中(io.writeFile 上的所有三个对应文件都在磁盘上更新)

All three functions look something like the below and using the debugger I can see that all three resolves are hit (and all three corresponding files on io.writeFile are updated on disk)

data.updateSharedLoot= function (lootUpdateChange) {
    return new Promise(function (resolve, reject) {
        //some logic
        io.writeFile(...,function(callbackSuccess){
            resolve(callbackSuccess);
        });
    });
}

推荐答案

这很奇怪,因为即使在旧的 Node 0.12 上我也定义了 Promise.all.在 Node 0.10 上,我没有定义 Promise.我认为没有 Promise 但没有 Promise.all 的版本.也许你正在做:

That's strange because I have Promise.all defined even on old Node 0.12. On Node 0.10 I have Promise not defined. I don't think there's a version with Promise but without Promise.all. Maybe you're doing:

Promise.all = undefined;

您应该未定义的是 resolve 函数.这里:

What you should have undefined is the resolve function. Here:

Promise.all(updates).then(function (success) {
    resolve("Item successfully used");
}, function (error) {
    resolve("Failed to use item " + error.toString());
});

您没有任何 resolve 可以调用.你不是说console.log吗?

you don't have any resolve to call. Don't you mean console.log?

Promise.all(updates).then(function (success) {
    console.log("Item successfully used");
}, function (error) {
    console.log("Failed to use item " + error.toString());
});

也在这里:

data.updateSharedLoot= function (lootUpdateChange) {
    return new Promise(function (resolve, reject) {
        //some logic
        io.writeFile(...,function(callbackSuccess){
            resolve(callbackSuccess);
        });
    });
}

回调的第一个参数可能是一个错误,所以你应该:

the first parameter to your callback is probably an error, so you should:

data.updateSharedLoot= function (lootUpdateChange) {
    return new Promise(function (resolve, reject) {
        //some logic
        io.writeFile(...,function(error, callbackSuccess) {
            if (error) {
                reject(error);
            } else {
                resolve(callbackSuccess);
            }
        });
    });
}

但我仍然建议使用承诺的 I/O 版本,例如 fs-promise 如果你无论如何都在做承诺.您返回承诺的函数可能很简单:

But still I would suggest using a promised version of I/O like fs-promise if you're doing promises anyway. Your function that returns a promise could be as simple as:

var fsp = require('fs-promise');

data.updateSharedLoot = function (lootUpdateChange) {
    return fsp.writeFile(...);
};

请参阅此答案了解更多详情.

这篇关于Node 中的 Promise.all 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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