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
68
votes
3 answers

Unit-test promise-based code in Angularjs

I'm having hard times trying to test promise-based code in Angularjs. I have the following code in my controller: $scope.markAsDone = function(taskId) { tasksService.removeAndGetNext(taskId).then(function(nextTask) { …
Michal Ostruszka
  • 2,089
  • 2
  • 20
  • 23
68
votes
4 answers

Angular JS: how to bind to promises

I am trying to bind a promise to a view. I don't know if you can do that directly, but that's what I'm attempting to do. Any ideas what I am doing wrong? Note: the source is a little contrived with the timeout and uses static data, but that's to…
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
67
votes
5 answers

Why is `.catch(err => console.error(err))` discouraged?

I'm using promises and have code that looks like the following: function getStuff() { return fetchStuff().then(stuff => process(stuff) ).catch(err => { console.error(err); }); } Or: async function getStuff() { try { const…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
67
votes
5 answers

fetch: Reject promise with JSON error object

I have an HTTP API that returns JSON data both on success and on failure. An example failure would look like this: ~ ◆ http get http://localhost:5000/api/isbn/2266202022 HTTP/1.1 400 BAD REQUEST Content-Length: 171 Content-Type:…
jbaiter
  • 6,913
  • 4
  • 30
  • 40
67
votes
1 answer

Is there a way to tell if an ES6 promise is fulfilled/rejected/resolved?

I'm used to Dojo promises, where I can just do the following: promise.isFulfilled(); promise.isResolved(); promise.isRejected(); Is there a way to determine if an ES6 promise is fulfilled, resolved, or rejected? If not, is there a way to fill in…
knpwrs
  • 15,691
  • 12
  • 62
  • 103
67
votes
6 answers

Error handling in AngularJS http get then construct

How can I handle an HTTP error, e.g. 500, when using the AngularJS "http get then" construct (promises)? $http.get(url).then( function(response) { console.log('get',response) } ) Problem is, for any non 200 HTTP response, the inner…
Federico Elles
  • 4,659
  • 7
  • 27
  • 35
67
votes
2 answers

JavaScript naming convention for promises?

I feel it would be useful to have a naming convention for JavaScript variables which hold a promise. I don't generally like or advocate naming conventions beyond programming language standards, but in the style of programming where promises are…
jevakallio
  • 35,324
  • 3
  • 105
  • 112
66
votes
8 answers

using a fetch inside another fetch in javascript

I want to get an api and after that call another one. Is it wisely using a code like this in javascript? fetch(url, { method: 'get', }).then(function(response) { response.json().then(function(data) { …
Dvlpr
  • 1,439
  • 1
  • 11
  • 21
66
votes
2 answers

How to check if a Promise is pending

I have this situation in which I would like to know what the status is of a promise. Below, the function start only calls someTest if it is not running anymore (Promise is not pending). The start function can be called many times, but if its called…
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
66
votes
3 answers

What does this error mean — Uncaught TypeError: Already read?

In Javascript, when is this error thrown? index.js /** * Created by tushar.mathur on 24/12/15. */ 'use strict' const _ = require('lodash') const Rx = require('rx') const createDataStore = require('./src/createDataStore') const fetch = x =>…
tusharmath
  • 10,622
  • 12
  • 56
  • 83
65
votes
4 answers

How to return from a Promise's catch/then block?

There are many tutorials on how to use "then" and "catch" while programming with JavaScript Promise. However, all these tutorials seem to miss an important point: returning from a then/catch block to break the Promise chain. Let's start with some…
lixiang
  • 1,941
  • 3
  • 20
  • 25
65
votes
5 answers

Catching Errors in JavaScript Promises with a First Level try ... catch

So, I want my first level catch to be the one that handles the error. Is there anyway to propagate my error up to that first catch? Reference code, not working (yet): Promise = require('./framework/libraries/bluebird.js'); function promise() { …
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177
64
votes
1 answer

Returning an Axios Promise from function

Can someone please explain why returning an Axios promise allows for further chaining, but returning after applying a then()/catch() method does not? Example: const url = 'https://58f58f38c9deb71200ceece2.mockapi.io/Mapss' function…
Matt Stone
  • 3,705
  • 4
  • 23
  • 40
64
votes
2 answers

Angular's $q.reject() vs deferred.reject()

I'm trying to get a handle on the Angular $q service and its related objects and APIs. When I look at the objects in my console I see: var deferred = $q.defer() ...(and then from console inspection)... $q: Object {defer: function, reject:…
lostdorje
  • 6,150
  • 9
  • 44
  • 86
63
votes
4 answers

How to wait for a stream to finish piping? (Nodejs)

I have a for loop array of promises, so I used Promise.all to go through them and called then afterwards. let promises = []; promises.push(promise1); promises.push(promise2); promises.push(promise3); Promise.all(promises).then((responses) => { …
ThePumpkinMaster
  • 2,181
  • 5
  • 22
  • 31