Friday, 26 May 2017

When start code in Promise in Node.js ES6?

I make a method that create a promise for each element in array.

queries.push(function (collection) {
    new Promise((resolve, reject) => {
        collection.find({}).limit(3).toArray(function (err, docs) {
            if (err) reject(err);
            resolve(docs);
        });
    });
});

const getAnalyticsPromises = (collection) => {
    let promises = [];
    queries.each((item) => {
        promises.push(item(collection));
    });
    console.log(queries);
    return promises;
}

This code return this errors:

(node:10464) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: queries.each is not a function
(node:10464) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The question is: When the promise is called? When i create it:

promises.push(item(collection));

or when after i call it with then() function?



via Pasalino

No comments:

Post a Comment