Questions tagged [promise]

Promises are a tactic for deferred computing, suitable for several styles of concurrency: thread and event loop concurrency for local computation, and both synchronous and asynchronous remote messaging. A promise represents the eventual result of an asynchronous operation. The primary way of working with promises is through a method which registers transformations from the promise's eventual value or failure reason to a new promise.

In computer science, future, promise, and delay refer to constructs used for synchronizing in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown, usually because the computation of its value is yet incomplete.

Promises are a common construct in with new built in language support. There are several popular implementations of the concept such as and . However, promises are not unique in JavaScript and similar patterns exist in many languages. Popular implementations exist in , , , , , , and most other languages. Some languages provide native language support for the construct.

Recurring questions:

Reading material:

Popular Implementations:

19807 questions
1750
votes
26 answers

Using async/await with a forEach loop

Are there any issues with using async/await in a forEach loop? I'm trying to loop through an array of files and await on the contents of each file. import fs from 'fs-promise' async function printFiles () { const files = await getFilePaths() //…
Saad
  • 32,434
  • 19
  • 55
  • 98
1582
votes
30 answers

What is the difference between Promises and Observables?

What is the difference between Promise and Observable in Angular? An example on each would be helpful in understanding both the cases. In what scenario can we use each case?
Rohit
  • 15,907
  • 3
  • 8
  • 9
768
votes
21 answers

How do I convert an existing callback API to promises?

I want to work with promises but I have a callback API in a format like: 1. DOM load or other one time event: window.onload; // set to callback ... window.onload = function() { }; 2. Plain callback: function request(onChangeHandler) { …
Benjamin Gruenbaum
  • 246,787
  • 79
  • 474
  • 476
697
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
  • 513,640
  • 108
  • 821
  • 1,164
653
votes
8 answers

Syntax for an async arrow function

I can mark a JavaScript function as "async" (i.e., returning a promise) with the async keyword. Like this: async function foo() { // Do something } What is the equivalent syntax for arrow functions?
BonsaiOak
  • 20,455
  • 5
  • 25
  • 50
563
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
  • 246,787
  • 79
  • 474
  • 476
485
votes
11 answers

jQuery deferreds and promises - .then() vs .done()

I've been reading about jQuery deferreds and promises and I can't see the difference between using .then() & .done() for successful callbacks. I know Eric Hynds mentions that .done() and .success() map to the same functionality but I'm guessing so…
screenm0nkey
  • 16,975
  • 17
  • 53
  • 75
484
votes
7 answers

JavaScript Promises - reject vs. throw

I have read several articles on this subject, but it is still not clear to me if there is a difference between Promise.reject vs. throwing an error. For example, Using Promise.reject return asyncIsPermitted() .then(function(result) { if…
Naresh
  • 18,757
  • 25
  • 99
  • 162
477
votes
19 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
  • 10,750
  • 4
  • 24
  • 31
464
votes
10 answers

Aren't promises just callbacks?

I've been developing JavaScript for a few years and I don't understand the fuss about promises at all. It seems like all I do is change: api(function(result){ api2(function(result2){ api3(function(result3){ // do work …
Benjamin Gruenbaum
  • 246,787
  • 79
  • 474
  • 476
406
votes
9 answers

What is std::promise?

I'm fairly familiar with C++11's std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward. However, I cannot quite grasp what std::promise is, what it does and in which situations it is best used. The…
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
394
votes
17 answers

How do I tell 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
  • 4,435
  • 2
  • 11
  • 14
351
votes
22 answers

Resolve Javascript Promise outside function 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
  • 6,142
  • 3
  • 20
  • 26
339
votes
5 answers

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)…
spirytus
  • 9,438
  • 12
  • 52
  • 74
330
votes
29 answers

Resolve promises one after another (i.e. in sequence)?

Consider the following code that reads an array of files in a serial/sequential manner. readFiles returns a promise, which is resolved only once all files have been read in sequence. var readFile = function(file) { ... // Returns a…
XåpplI'-I0llwlg'I -
  • 18,987
  • 23
  • 96
  • 146
1
2 3
99 100