How can I synchronously determine a JavaScript Promise’s state?

I have a pure JavaScript Promise (built-in implementation or poly-fill): var promise = new Promise(function (resolve, reject) { /* … */ }); From the specification, a Promise can be one of: ‘settled’ and ‘resolved’ ‘settled’ and ‘rejected’ ‘pending’ I have a use case where I wish to interrogate the Promise synchronously and determine: is the … 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

What is an unhandled promise rejection?

For learning Angular 2, I am trying their tutorial. I am getting an error like this: (node:4796) UnhandledPromiseRejectionWarning: Unhandled promise rejection (r ejection id: 1): Error: spawn cmd ENOENT [1] (node:4796) 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 … 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

How to reject in async/await syntax?

How can I reject a promise that returned by an async/await function? e.g. Originally: foo(id: string): Promise<A> { return new Promise((resolve, reject) => { someAsyncPromise().then((value)=>resolve(200)).catch((err)=>reject(400)) }); } Translate into async/await: async foo(id: string): Promise<A> { try{ await someAsyncPromise(); return 200; } catch(error) {//here goes if someAsyncPromise() rejected} return 400; //this will result in a resolved … Read more

Resolve Javascript Promise outside the Promise constructor scope

I have been using ES6 Promise. Ordinarily, a Promise is constructed and used like this new Promise(function(resolve, reject){ if (someCondition){ resolve(); } else { reject(); } }); But I have been doing something like below to take the resolve outside for the sake of flexibility. var outsideResolve; var outsideReject; new Promise(function(resolve, reject) { outsideResolve = … Read more