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
119
votes
6 answers

Using success/error/finally/catch with Promises in AngularJS

I'm using $http in AngularJs, and I'm not sure on how to use the returned promise and to handle errors. I have this code: $http .get(url) .success(function(data) { // Handle data }) .error(function(data, status) { //…
Joel
  • 8,502
  • 11
  • 66
  • 115
118
votes
23 answers

Promise Retry Design Patterns

Edit Pattern that keep on retrying until the promise resolves (with delay and maxRetries). Pattern that keeps on retrying until the condition meets on the result (with delay and maxRetries). A memory efficient dynamic Pattern with unlimited retries…
user2727195
  • 7,122
  • 17
  • 70
  • 118
117
votes
7 answers

What does $.when.apply($, someArray) do?

I'm reading about Deferreds and Promises and keep coming across $.when.apply($, someArray). I'm a little unclear on what this does exactly, looking for an explanation that one line works exactly (not the entire code snippet). Here's some context:…
manafire
  • 5,984
  • 4
  • 43
  • 53
116
votes
9 answers

How do you properly return multiple values from a Promise?

I've recently run into a certain situation a couple of times, which I didn't know how to solve properly. Assume the following code: somethingAsync() .then( afterSomething ) .then( afterSomethingElse ) function afterSomething( amazingData ) { …
Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
113
votes
6 answers

Creating a (ES6) promise without starting to resolve it

Using ES6 promises, how do I create a promise without defining the logic for resolving it? Here's a basic example (some TypeScript): var promises = {}; function waitFor(key: string): Promise { if (key in promises) { return promises[key]; …
Barguast
  • 5,926
  • 9
  • 43
  • 73
111
votes
3 answers

Promise.all().then() resolve?

Using Node 4.x. When you have a Promise.all(promises).then() what is the proper way to resolve the data and pass it to the next .then()? I want to do something like this: Promise.all(promises).then(function(data){ // Do something with the data…
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
109
votes
8 answers

async/await in Angular `ngOnInit`

I’m currently evaluating the pros ‘n’ cons of replacing Angular’s resp. RxJS’ Observable with plain Promise so that I can use async and await and get a more intuitive code style. One of our typical scenarios: Load some data within ngOnInit. Using…
qqilihq
  • 10,794
  • 7
  • 48
  • 89
109
votes
4 answers

When is the body of a Promise constructor callback executed?

Suppose I have the following code constructing a Promise: function doSomethingAsynchronous() { return new Promise((resolve) => { const result = doSomeWork(); setTimeout(() => { resolve(result); }), 100); }); } At which point…
Kevin
  • 14,655
  • 24
  • 74
  • 124
109
votes
1 answer

Does never resolved promise cause memory leak?

I have a Promise. I created it to cancel an AJAX request if needed. But since I don't need to cancel that AJAX, I've never resolved it and AJAX completed successfully. A simplified snippet: var defer = $q.defer(); $http({url: 'example.com/some/api',…
Umut Benzer
  • 3,476
  • 4
  • 36
  • 53
108
votes
6 answers

Wait for all promises to resolve

So I have a situation where I have multiple promise chains of an unknown length. I want some action to run when all the CHAINS have been processed. Is that even possible? Here is an example: app.controller('MainCtrl', function($scope, $q, $timeout)…
jensengar
  • 6,117
  • 17
  • 58
  • 90
106
votes
3 answers

Angularjs $q.all

I have implemented the $q.all in angularjs, but I can not make the code work. Here is my code : UploadService.uploadQuestion = function(questions){ var promises = []; for(var i = 0 ; i < questions.length ; i++){ var…
themyth92
  • 1,743
  • 2
  • 17
  • 23
105
votes
1 answer

Why does TypeScript use "Like" types?

Why does TypeScript have a type and then a "like type"? An example of this is Promise and PromiseLike. What are the differences between these two types? When should I use them? In this case why not just have one Promise type?
Stewart
  • 3,023
  • 2
  • 24
  • 40
104
votes
2 answers

How to reject a promise from inside then function

This is probably a silly question, but mid promise chain, how do you reject a promise from inside one of the then functions? For example: someActionThatReturnsAPromise() .then(function(resource) { return modifyResource(resource) }) …
chinabuffet
  • 5,278
  • 9
  • 40
  • 64
104
votes
6 answers

Is there any analog to a 'finally' in jQuery AJAX calls?

Is there a Java 'finally' analogue in jQuery AJAX calls? I have this code here. In my always I throw an exception, however I ALWAYS want it to go to the then() method. call.xmlHttpReq = $.ajax({ url : url, dataType : 'json', …
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
102
votes
7 answers

Typescript Promise rejection type

How do I set the type of the rejection of my promise? Let's say I do: const start = (): Promise => { return new Promise((resolve, reject) => { if (someCondition) { resolve('correct!'); } else { reject(-1); …
Kousha
  • 32,871
  • 51
  • 172
  • 296