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
88
votes
7 answers

How can I fetch an array of URLs with Promise.all?

If I have an array of urls: var urls = ['1.txt', '2.txt', '3.txt']; // these text files contain "one", "two", "three", respectively. And I want to build an object that looks like this: var text = ['one', 'two', 'three']; I’ve been trying to learn…
user2467065
87
votes
4 answers

Is it a good practice using Observable with async/await?

I am using angular 2 common http that return an Observable, but I face with a problem that my code likes a mesh when I use nested Observable call: this.serviceA.get().subscribe((res1: any) => { this.serviceB.get(res1).subscribe((res2: any) => { …
Thach Huynh
  • 1,173
  • 2
  • 12
  • 24
87
votes
5 answers

How to Check Whether an Angular $q promise Is Resolved

I understand that typically one would just attach continuation code with a then() call and chain behaviour when using promises. However, I want to kick off a promise-wrapped asynchronous call and then separately kick off a 3-second $timeout() so I…
blaster
  • 8,876
  • 11
  • 48
  • 77
86
votes
1 answer

error TS2794: Expected 1 arguments, but got 0. Did you forget to include 'void' in your type argument to 'Promise'?

In my project, I was using Typescript@4.0.3 and it was working fine, but now I updated its version to latest Typescript@4.1.3 and it is giving me a lot of errors. I am unable to find anything in documentations and not getting any Idea how to resolve…
Naila Akbar
  • 3,033
  • 4
  • 34
  • 76
86
votes
2 answers

Deferred versus promise

What is the difference between Deferred and Promise other than the jQuery versions? What should I use for my need? I only want to call the fooExecute(). I only need the fooStart() and fooEnd() to toggle the html div status for example. //I'm using…
fletchsod
  • 3,560
  • 7
  • 39
  • 65
85
votes
2 answers

Chaining Observables in RxJS

I'm learning RxJS and Angular 2. Let's say I have a promise chain with multiple async function calls which depend on the previous one's result which looks like: var promiseChain = new Promise((resolve, reject) => { setTimeout(() => { …
Harindaka
  • 4,658
  • 8
  • 43
  • 62
85
votes
2 answers

How to support promises in Internet Explorer 11?

I have a simple code that runs perfectly on every browser except for the Internet Explorer 11. How can I make it work on all browsers? Codepen 'use strict'; let promise = new Promise((resolve, reject) => { setTimeout(() => { …
85
votes
8 answers

Are nested promises normal in Node.js?

The problem I have been struggling with for two weeks now while learning Node.js is how to do synchronous programming using node. I found that no matter how I try to do things sequentially I always end up with nested promises. I have found that…
Grim
  • 2,398
  • 4
  • 35
  • 54
85
votes
6 answers

Use Promise to wait until polled condition is satisfied

I need to create a JavaScript Promise that will not resolve until a specific condition is true. Let's say I have a 3rd party library, and I need to wait until a certain data condition exists within that library. The scenario I am interested in is…
Joseph Gabriel
  • 8,339
  • 3
  • 39
  • 53
84
votes
4 answers

Error: Type is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor

I've written this function in TypeScript: export class LoginService { async isLoggedIn(): boolean { const r = await this.http.get('http://localhost:3000/api/user/isLoggedIn').toPromise(); return r.body; } } When I try to run the…
user275801
  • 1,235
  • 1
  • 10
  • 13
83
votes
5 answers

How to catch uncaught exception in Promise

Is there any way to globally catch all exceptions including Promise exceptions. Example: window.onerror = function myErrorHandler(errorMsg, url, lineNumber) { alert("Error occured: " + errorMsg);//or any message return false; …
John
  • 2,494
  • 5
  • 21
  • 24
83
votes
4 answers

How to always run some code when a promise is fulfilled in Angular.js

In my Angular.js application, I'm running some asynchronous operation. Before it starts I cover the application with a modal div, then once the operation is complete, I need to remove the div, whether the operation was successful or not. Currently I…
laurent
  • 88,262
  • 77
  • 290
  • 428
82
votes
6 answers

How to debug javascript promises?

I am trying to understand how to debug asynchronous code that is based on promises. By Promises I mean ECMAScript 6 based promises and by debugging I mean using the built-in chrome or firefox debugger. What I am having trouble with - is that when an…
YemSalat
  • 19,986
  • 13
  • 44
  • 51
82
votes
14 answers

While loop with promises

What would be the idiomatic way to do something like a while loop with promises. So: do something if the condition still stands do it again repeat then do something else. dosomething.then(possilblydomoresomethings).then(finish) I've done it this…
Grummle
  • 1,364
  • 1
  • 12
  • 21
81
votes
10 answers

Is there a way to short circuit async/await flow?

All four functions are called below in update return promises. async function update() { var urls = await getCdnUrls(); var metadata = await fetchMetaData(urls); var content = await fetchContent(metadata); await render(content); …
sbr
  • 4,735
  • 5
  • 43
  • 49