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
5
votes
2 answers

Should I always return a promise when I have an async function?

When I write an async function it usually returns a promise: export const myPromiseFunction = async (params) => { // some logic return Promise.resolve('resolved-value'); }); But I was wondering if it would be a mistake if this function would…
fewfew
  • 231
  • 2
  • 10
5
votes
1 answer

error TS2468: Cannot find global value 'Promise'

I know this question is asked before but solutions doesn't fix my problem. That's why I wanted to ask again. Because I have tried with ES5, ES6, ES2018, ES2015, ES2015.promise etc.. Background.js async function run() { setInterval(()=>{ …
fobus
  • 1,938
  • 8
  • 29
  • 48
5
votes
2 answers

Should I define async function if I explicitly return a Promise?

We know that async functions return a Promise implicitly. But I have a purely pedantic question. Should I put an async keyword if I return a Promise explicitly? Is this: const wait = async ms => new Promise( resolve => setTimeout(resolve,…
Robo Robok
  • 21,132
  • 17
  • 68
  • 126
5
votes
5 answers

How to wait for Cypress then() command to finish before returning a value?

I'm trying to set a variable within a .then() command which is declared outside of it, and after the whole block finished (the .then()) I'm returning that value. The problem is, when I return the value, the variable is undefined, but within the…
Jorge
  • 182
  • 1
  • 4
  • 12
5
votes
1 answer

What is the difference between async/await forEach and Promise.all + map

In the accepted answer to a similar question the answer states that a forEach call just throw a promise then exit. I think this should be the case as forEach returns undefined but why does the following code work? const networkFunction = (callback)…
Abdullah Khaled
  • 309
  • 1
  • 2
  • 11
5
votes
3 answers

Can you return a Promise with a predefined finally?

I have a function that returns a Promise. When I am done using that Promise in the .then() or .catch() blocks, I always want to execute the same cleanup code. My current setup is this: const promiseWithFinally = () => { return new…
DaDo
  • 443
  • 1
  • 6
  • 20
5
votes
2 answers

Type 'boolean' is not assignable to type 'Promise'

I'm using inApp purchase and I want to change the UI to 'unavailable' if for some reason I'm unable to retrieve the product information from Google or iOS app store. ionViewDidEnter() { this.platform.ready().then(async () => { …
D.Hodges
  • 1,794
  • 4
  • 24
  • 47
5
votes
3 answers

Component not updating after fetching new array

I'm new to ReactJS and to JS in general. I have a table that populates with data from an API request. Each row has a delete button that deletes that specific entry. When you press the delete button a Modal opens asking you to confirm the operation,…
5
votes
2 answers

C++ returning a future

I'm using google test and am trying to mock a method that returns a future. What I'm doing is creating some future object in a test case, then I do "when(mock.Call()).thenReturn(myFutureObject), but I get an error "use of deleted function…
linnoob
  • 81
  • 1
  • 7
5
votes
4 answers

Vue Js : This dependency was not found: * core-js/fn/promise

I have an Vuejs App using promise but get this error : This dependency was not found: core-js/fn/promise in ./src/store/index.js To install it, you can run: npm install --save core-js/fn/promise my package.json: "dependencies": { "axios":…
Sermed mayi
  • 697
  • 7
  • 26
5
votes
2 answers

Difference between JavaScript and Python Promise / Awaitable Evaluation

I've been trying to learn the JS concurrency model given a background in Python's. Running the following in Python: async def myFunction(): print("abc") myFunction() will print nothing. Running the following in JavaScript: async function…
Mario Ishac
  • 5,060
  • 3
  • 21
  • 52
5
votes
2 answers

Is it OK to pass an async function to setImmediate in order to call an arbitrary function asynchronously?

I want to call a given function asynchronously. The wrapper function tryCallAsync is one way of doing this. This approach works. However, it requires that the callback for setImmediate to be an async function. This seems wrong, as the callback…
stackhatter
  • 304
  • 2
  • 11
5
votes
1 answer

How does the event loop in the browser deal with the event queue, job queue, render queue at the same time?

Last week I started learning Javascript back-end with nodejs. While working with async functions, I wanted to understand this in all aspect and started doing research on this subject. I found Jake Archibald's presentation in jsconf and I tried to…
BarkinM
  • 51
  • 2
5
votes
1 answer

should I use fetch() with .then or async/await

The following pieces of code are giving me the same result. What is the difference between the use of .then and async/await to fetch data? // Code 1 function fetchData() { fetch(url) .then(response => response.json()) .then(json =>…
Leandro
  • 51
  • 1
  • 2
5
votes
2 answers

Multithreading with javascript promises

Just learning promises. Javascript is single threaded right? So when it uses fetch api to make http requests it all happens in one thread? How does it manage concurrency with PromisePool then? var p =…
TheWommies
  • 4,922
  • 11
  • 61
  • 79
1 2 3
99
100