Skip to content Skip to sidebar Skip to footer

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 a

Solution 1:

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) {
    returnnewPromise(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) {
    returnnewPromise(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.

Post a Comment for "Promise.all In Node Is Undefined"