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

What do double brackets mean in javascript and how to access them

Situation I have the following function which uses Promise. var getDefinitions = function() { return new Promise(function(resolve) { resolve(ContactManager.request("definition:entities")); }); } var definitions =…
Jeff
  • 3,943
  • 8
  • 45
  • 68
79
votes
8 answers

Native Support for Promises in Node.js

Is there native support for promises in current versions of Node.js? Node.js uses the V8 engine. This JavaScript engine is also used by Chrome, and Chrome 32 has native support for promises. But I can't seem to get promises to work (natively) in…
Carl Parker
  • 1,005
  • 1
  • 8
  • 10
77
votes
9 answers

using async await and .then together

Is there any harm in using async/await and .then().catch() together such as: async apiCall(params) { var results = await this.anotherCall() .then(results => { //do any results transformations return results; }) …
PixelPaul
  • 1,022
  • 1
  • 10
  • 18
77
votes
8 answers

NodeJS Timeout a Promise if failed to complete in time

How can I timeout a promise after certain amount of time? I know Q has a promise timeout, but I'm using native NodeJS promises and they don't have .timeout function. Am I missing one or its wrapped differently? Alternatively, Is the below…
AlexD
  • 4,062
  • 5
  • 38
  • 65
76
votes
4 answers

AngularJS resource promise

I've got a simple controller that use $resource : var Regions = $resource('mocks/regions.json'); $scope.regions = Regions.query(); I'm using this controller in a directive (in the link function) var regions = scope.regions; But regions is…
Thomas Pons
  • 7,709
  • 3
  • 37
  • 55
75
votes
6 answers

Wait for Angular 2 to load/resolve model before rendering view/template

In Angular 1.x, UI-Router was my primary tool for this. By returning a promise for "resolve" values, the router would simply wait for the promise to complete before rendering directives. Alternately, in Angular 1.x, a null object will not crash a…
shannon
  • 8,664
  • 5
  • 44
  • 74
75
votes
5 answers

How is a promise/defer library implemented?

How is a promise/defer library like q implemented? I was trying to read the source code but found it pretty hard to understand, so I thought it'd be great if someone could explain to me, from a high level, what are the techniques used to implement…
Derek Chiang
  • 3,330
  • 6
  • 27
  • 34
74
votes
3 answers

Why is `Promise.then` called twice in a React component but not the console.log?

I'm very confused about the output of the following component: import { StrictMode } from "react" import ReactDOM from "react-dom" function Test(): React.ReactElement { console.log('render') Promise.resolve() .then(() =>…
Anton Bessonov
  • 9,208
  • 3
  • 35
  • 38
74
votes
4 answers

angular $q, How to chain multiple promises within and after a for-loop

I want to have a for-loop which calls async functions each iteration. After the for-loop I want to execute another code block, but not before all the previous calls in the for-loop have been resolved. My problem at the moment is, that either the…
SebastianRiemer
  • 1,495
  • 2
  • 20
  • 33
73
votes
8 answers

Promise All with Axios

I just read an Article related to promise and was unable to comprehend how we can do multiple API call using Axios via Promise.all So consider there are 3 URL, lets call it something like this let URL1 = "https://www.something.com" let URL2 =…
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210
73
votes
4 answers

How do I properly test for a rejected promise using Jest?

Code import { createUser } from '../services'; ... ... handleFormSubmit = () => { this.setState({ loading: true }); createUser() .then(() => { this.setState({ loading: false, }); }) .catch(e => { …
coding123
  • 823
  • 1
  • 8
  • 12
73
votes
5 answers

async/await always returns promise

I'm trying async/await functionality. I have such code imitating a request: const getJSON = async () => { const request = () => new Promise((resolve, reject) => ( setTimeout(() => resolve({ foo: 'bar'}), 2000) )); const json = await…
user3309314
  • 2,453
  • 2
  • 17
  • 30
71
votes
5 answers

Promise reject() causes "Uncaught (in promise)" warning

Once a promise reject() callback is called, a warning message "Uncaught (in promise)" appears in the Chrome console. Yet I have a catch handler in place. I can't wrap my head around the reason behind it, nor how to get rid of it. var p = new…
Yevgeny Kozlov
  • 1,151
  • 1
  • 8
  • 11
69
votes
5 answers

Promise inside promise: what's the correct way to return a variable from the child promise? (JS)

I've a function like this: function top() { //promise1 ParentPromise({ ...some code here... }).then(function() { //promise2 ChildPromise({ ..some code here... }).then(function(response) { var…
Valerio Marzulli
  • 905
  • 1
  • 8
  • 13
69
votes
5 answers

Basic Javascript promise implementation attempt

To gain better understanding of how promises work in Javascript I decided to give it a try and code basic implementation myself. Basically I want to implement Promises Object (I call it Aaa in my code) that takes function as an argument. This…
spirytus
  • 10,726
  • 14
  • 61
  • 75