How to access the value of a promise?

I’m looking at this example from Angular’s docs for $q but I think this probably applies to promises in general. The example below is copied verbatim from their docs with their comment included: promiseB = promiseA.then(function(result) { return result + 1; }); // promiseB will be resolved immediately after promiseA is resolved and its value … Read more

Are there still reasons to use promise libraries like Q or BlueBird now that we have ES6 promises? [closed]

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago. Improve this question After Node.js added native support for promises, are there still reasons to use libraries like Q … Read more

Is Node.js native Promise.all processing in parallel or sequentially?

I would like to clarify this point, as the documentation is not too clear about it; Q1: Is Promise.all(iterable) processing all promises sequentially or in parallel? Or, more specifically, is it the equivalent of running chained promises like p1.then(p2).then(p3).then(p4).then(p5)…. or is it some other kind of algorithm where all p1, p2, p3, p4, p5, etc. … Read more

Promise.all: Order of resolved values

Looking at MDN it looks like the values passed to the then() callback of Promise.all contains the values in the order of the promises. For example: var somePromises = [1, 2, 3, 4, 5].map(Promise.resolve); return Promise.all(somePromises).then(function(results) { console.log(results) // is [1, 2, 3, 4, 5] the guaranteed result? }); Can anybody quote a spec stating … Read more

Do I need to return after early resolve/reject?

Suppose I have the following code. function divide(numerator, denominator) { return new Promise((resolve, reject) => { if(denominator === 0){ reject(“Cannot divide by 0”); return; //superfluous? } resolve(numerator / denominator); }); } If my aim is to use reject to exit early, should I get into the habit of returning immediately afterward as well? 6 Answers … Read more

Use async await with Array.map

Given the following code: var arr = [1,2,3,4,5]; var results: number[] = await arr.map(async (item): Promise<number> => { await callAsynchronousOperation(item); return item + 1; }); which produces the following error: TS2322: Type ‘Promise<number>[]’ is not assignable to type ‘number[]’. Type ‘Promise<number> is not assignable to type ‘number’. How can I fix it? How can I … Read more

What’s the difference between returning value or Promise.resolve from then()

What is the difference between: new Promise(function(res, rej) { res(“aaa”); }) .then(function(result) { return “bbb”; }) .then(function(result) { console.log(result); }); and this: new Promise(function(res, rej) { res(“aaa”); }) .then(function(result) { return Promise.resolve(“bbb”); }) .then(function(result) { console.log(result); }); I’m asking as I’m getting different behaviour Using Angular and $http service with chaining .then(). A bit too … Read more