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

nodejs - How to promisify http.request? reject got called two times

I'm trying to wrap http.request into Promise: new Promise(function(resolve, reject) { var req = http.request({ host: '127.0.0.1', port: 4000, method: 'GET', path: '/api/v1/service' }, function(res) { …
happy_marmoset
  • 2,137
  • 3
  • 20
  • 25
101
votes
6 answers

Promise equivalent in C#

In Scala there is a Promise class that could be used to complete a Future manually. I am looking for an alternative in C#. I am writing a test and I want it to look it similar to this: // var MyResult has a field `Header` var promise = new…
eddyP23
  • 6,420
  • 7
  • 49
  • 87
100
votes
6 answers

How to specify resolution and rejection type of the promise in JSDoc?

I have some code that returns a promise object, e.g. using Q library for NodeJS. var Q = require('q'); /** * @returns ??? */ function task(err) { return err? Q.reject(new Error('Some error')) : Q.resolve('Some result'); } How to document…
Arikon
  • 1,213
  • 2
  • 9
  • 10
99
votes
2 answers

fromPromise does not exist on type Observable

In Angular 2 using rxjs I was trying to convert a Promise to Observable. As many of online guides showed I used fromPromise on Observable. Which throws error: Property 'fromPromise' does not exist on type 'typeof Observable'. Observable was…
Ahmad
  • 2,629
  • 3
  • 22
  • 27
98
votes
10 answers

vue-router — Uncaught (in promise) Error: Redirected from "/login" to "/" via a navigation guard

Why is vue-router giving me this error? To be clear, the login flow works as intended but I want to a) get rid of the errro and b) understand why the error is happening. Error: Uncaught (in promise) Error: Redirected from "/login" to "/" via…
Kirk Ross
  • 6,413
  • 13
  • 61
  • 104
98
votes
5 answers

Using promise function inside Javascript Array map

Having an array of objects [obj1, obj2] I want to use Map function to make a DB query (that uses promises) about all of them and attach the results of the query to each object. [obj1, obj2].map(function(obj){ …
Jorge
  • 2,156
  • 2
  • 22
  • 29
97
votes
3 answers

How to handle the if-else in promise then?

In some case, when I get a return value from a promise object, I need to start two different then() precesses depend on the value's condition, like: promise().then(function(value){ if(//true) { // do something } else { // do…
Brick Yang
  • 5,388
  • 8
  • 34
  • 45
96
votes
3 answers

Axios Interceptors retry original request and access original promise

I have an interceptor in place to catch 401 errors if the access token expires. If it expires it tries the refresh token to get a new access token. If any other calls are made during this time they are queued until the access token is…
Tim Wickstrom
  • 5,476
  • 3
  • 25
  • 33
95
votes
8 answers

Replacing callbacks with promises in Node.js

I have a simple node module which connects to a database and has several functions to receive data, for example this function: dbConnection.js: import mysql from 'mysql'; const connection = mysql.createConnection({ host: 'localhost', user:…
Lior Erez
  • 1,852
  • 2
  • 19
  • 24
94
votes
2 answers

How to add delay to promise inside then

fetch() { return axios.get('/rest/foo') //.then(response => {throw new Error(response)}) // Uncomment to test network error //.then( <> ) // Uncomment to simulate network delay } How do I add delay in the…
Tuomas Toivonen
  • 21,690
  • 47
  • 129
  • 225
94
votes
8 answers

Angular 2: Convert Observable to Promise

Q) How do I convert the following observable to a promise so I can call it with .then(...)? My method I want to convert to a promise: this._APIService.getAssetTypes().subscribe( assettypes => { …
Dave
  • 5,283
  • 7
  • 44
  • 66
91
votes
6 answers

How do Clojure futures and promises differ?

Both futures and promises block until they have calculated their values, so what is the difference between them?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
91
votes
2 answers

return value after a promise

I have a javascript function where I want to return the value that I get after the return method. Easier to see than explain function getValue(file){ var val; lookupValue(file).then(function(res){ val = res.val; } return…
pedalpete
  • 21,076
  • 45
  • 128
  • 239
90
votes
9 answers

How can I execute array of promises in sequential order?

I have an array of promises that need to run in sequential order. var promises = [promise1, promise2, ..., promiseN]; Calling RSVP.all will execute them in parallel: RSVP.all(promises).then(...); But, how can I run them in sequence? I can…
jaaksarv
  • 1,440
  • 1
  • 11
  • 16
89
votes
6 answers

How to cancel a HTTPRequest in Angular 2?

How to cancel a HTTPRequest in Angular 2? I know how to reject the request promise only. return new Promise((resolve, reject) => { this.currentLoading.set(url, {resolve, reject}); this.http.get(url, {headers: reqHeaders}) …
tom10271
  • 4,222
  • 5
  • 33
  • 62