Questions tagged [es6-promise]

An ES6 Promise is an ECMAScript 2015 object that represents an in-progress asynchronous operation

An ES6 Promise is an ECMAScript 2015 (ES6) object that represents the eventual completion or failure of an asynchronous operation. The ES6 Promise API is based on the Promises/A+ specification.

Consumer-side, a promise is essentially a returned object to attach callbacks to, instead of passing callbacks into a function. The .then function lets callers chain operations to be run subsequently.

In an ideal world, all asynchronous functions would return promises. However, a promise may also be created from scratch using its constructor, to wrap old callback-style function calls.

Promise objects have three possible states:

  • Pending
  • Fulfilled
  • Rejected

Resources

Related Tages

5243 questions
748
votes
17 answers

How do I access previous promise results in a .then() chain?

I have restructured my code to promises, and built a wonderful long flat promise chain, consisting of multiple .then() callbacks. In the end I want to return some composite value, and need to access multiple intermediate promise results. However the…
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
647
votes
3 answers

What is the explicit promise construction antipattern and how do I avoid it?

I was writing code that does something that looks like: function getStuffDone(param) { | function getStuffDone(param) { var d = Q.defer(); /* or $q.defer */ | return new Promise(function(resolve, reject) { // or = new…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
572
votes
20 answers

Wait until all promises complete even if some rejected

Let's say I have a set of Promises that are making network requests, of which one will fail: // http://does-not-exist will throw a TypeError var arr = [ fetch('index.html'), fetch('http://does-not-exist') ] Promise.all(arr) .then(res =>…
Nathan Hagen
  • 12,440
  • 4
  • 26
  • 31
500
votes
27 answers

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…
Morio
  • 8,463
  • 5
  • 25
  • 29
498
votes
19 answers

How to check if an object is a Promise?

Whether it's an ES6 Promise or a Bluebird Promise, Q Promise, etc. How do I test to see if a given object is a Promise?
theram
  • 5,503
  • 2
  • 13
  • 14
464
votes
8 answers

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 { return new Promise((resolve, reject) => { someAsyncPromise().then((value)=>resolve(200)).catch((err)=>reject(400)) …
Phoenix
  • 4,773
  • 2
  • 10
  • 6
430
votes
22 answers

Handling errors in Promise.all

I have an array of Promises that I'm resolving with Promise.all(arrayOfPromises); I go on to continue the promise chain. Looks something like this existingPromiseChain = existingPromiseChain.then(function() { var arrayOfPromises =…
Jon
  • 5,945
  • 4
  • 21
  • 31
403
votes
6 answers

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); …
sam
  • 4,033
  • 2
  • 10
  • 4
316
votes
16 answers

Axios get access to response header fields

I'm building a frontend app with React and Redux and I'm using axios to perform my requests. I would like to get access to all the fields in the header of the response. In my browser I can inspect the header and I can see that all the fields that I…
TWONEKSONE
  • 3,918
  • 3
  • 19
  • 26
308
votes
9 answers

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 …
305
votes
3 answers

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…
Thorben Croisé
  • 12,407
  • 8
  • 39
  • 50
300
votes
14 answers

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…
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
241
votes
31 answers

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…
jokeyrhyme
  • 3,258
  • 2
  • 20
  • 20
208
votes
32 answers

What is the best way to limit concurrency when using ES6's Promise.all()?

I have some code that is iterating over a list that was queried out of a database and making an HTTP request for each element in that list. That list can sometimes be a reasonably large number (in the thousands), and I would like to make sure I am…
Chris
  • 4,663
  • 5
  • 24
  • 33
202
votes
10 answers

JavaScript ES6 promise for loop

for (let i = 0; i < 10; i++) { const promise = new Promise((resolve, reject) => { const timeout = Math.random() * 1000; setTimeout(() => { console.log(i); }, timeout); }); // TODO: Chain this promise…
Poni
  • 11,061
  • 25
  • 80
  • 121
1
2 3
99 100