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:

22514 questions
2999
votes
33 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
  • 49,729
  • 21
  • 73
  • 112
1848
votes
34 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
  • 18,575
  • 3
  • 11
  • 9
907
votes
11 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
  • 27,741
  • 7
  • 30
  • 54
860
votes
24 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
  • 270,886
  • 87
  • 504
  • 504
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
626
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
  • 23,937
  • 33
  • 132
  • 204
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
519
votes
12 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
  • 18,405
  • 17
  • 57
  • 75
514
votes
9 answers

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 => { await callAsynchronousOperation(item); return item + 1; }); which produces the following error: TS2322:…
Alon
  • 10,381
  • 23
  • 88
  • 152
507
votes
11 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
  • 270,886
  • 87
  • 504
  • 504
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
447
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
  • 464,522
  • 92
  • 875
  • 1,084
414
votes
36 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 -
  • 21,649
  • 28
  • 102
  • 151
1
2 3
99 100