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

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

问题描述

我正在玩Node.js中的promises,我正在尝试使用Promise.all。我正在推送三个函数,它们将一个promise返回到一个数组中,然后调用Promise.all但是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);
        });
    });
}


推荐答案

这很奇怪因为我有承诺.all甚至在旧的节点0.12上定义。在节点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());
});

您没有任何解决到呼叫。你的意思是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);
        });
    });
}

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

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天全站免登陆