Promise.all is returning an array of undefined and resolves before being done

Promise.all accepts an Array of Promise objects. You’re getting an Array of undefined because you’re not returning any Promise in your map callback: function addText(queries) { return Promise.all(queries.map(function(query) { // Add `return` here or the `map` returns an Array of `undefined`. return models.queries .findById(query.queryId, { raw: true, attributes: [ “query” ] }) .then(function(queryFetched) { query.text … Read more

How to chain a variable number of promises in Q, in order?

There’s a nice clean way to to this with [].reduce. var chain = itemsToProcess.reduce(function (previous, item) { return previous.then(function (previousValue) { // do what you want with previous value // return your async operation return Q.delay(100); }) }, Q.resolve(/* set the first “previousValue” here */)); chain.then(function (lastResult) { // … }); reduce iterates through the … Read more

How do you wrap setTimeout in a promise [duplicate]

TL;DR – you’ve wrapped setTimeout in a promise properly, the issue is you are using it improperly .then(promiseTimeout(2000)).then will not do what you expect. The “signature” for .then is then(functionResolved, functionRejected) A promise’s then method accepts two arguments: promise.then(onFulfilled, onRejected) Both onFulfilled and onRejected are optional arguments: If onFulfilled is not a function, it must … Read more

Can I catch an error from async without using await?

Dealing with unhandled rejected native promises (and async/await uses native promises) is a feature supported now in V8. It’s used in the latest Chrome to output debugging information when a rejected promise is unhandled; try the following at the Babel REPL: async function executor() { console.log(“execute”); } async function doStuff() { console.log(“do stuff”); throw new … Read more

Creating a (ES6) promise without starting to resolve it

Good question! The resolver passed to the promise constructor intentionally runs synchronous in order to support this use case: var deferreds = []; var p = new Promise(function(resolve, reject){ deferreds.push({resolve: resolve, reject: reject}); }); Then, at some later point in time: deferreds[0].resolve(“Hello”); // resolve the promise with “Hello” The reason the promise constructor is given … Read more

Why is my function returning Promise { } [duplicate]

const postCount = boards.map(async (boardI) => { const posts = await Post.find({ board: boardI.slug }); return [boardI.slug, posts.length]; }); Since this is an async function, it will return a promise. map calls the function for each element of the array, gets the promises they return, and creates a new array containing those promises. If you … Read more

Promise and Promise.all(array) executed before array fulfilled [duplicate]

As georg said, you’re just not putting the promises in arr, because you never return anything from the anonymous function you’ve wrapped around new Promise. That function is also completely unnecessary, so: var arr = []; for (var i = 0; i < 2; i++) { arr.push(new Promise(function(resolve, reject) { setTimeout(function() { log(‘resolved !’); resolve(); … Read more