Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

javascript - Promise.all in Node is undefined

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".

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

Relevant part of the code:

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());
});

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);
        });
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

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());
});

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());
});

Also here:

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);
            }
        });
    });
}

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(...);
};

See this answer for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...