Linked Questions

6714 votes
42 answers
2.1m views

How do I return the response from an asynchronous call?

How do I return the response/result from a function foo that makes an asynchronous request? I am trying to return the value from the callback, as well as assigning the result to a local variable ...
Felix Kling's user avatar
2 votes
1 answer
8k views

Return a value from a setTimeout [duplicate]

I'm trying to return a value calculated in a setTimout. But it seems like I can't do get this value. I read that to do this I must use promises. Am I obliged to use this like this exemple : https://...
Guillaume's user avatar
  • 1,600
2 votes
2 answers
501 views

How can I get the return value from an `setTimeout` callback? [duplicate]

consider this code const obj = { generate(num) { return Math.random()*num; }, add(a,b) { return this.generate(a) + this.generate(b) } }; function delay(func, ms) { function wrapper(...
Joji's user avatar
  • 5,483
8 votes
5 answers
2k views

Simple Promise and Then implementation

Recently I was shown a piece of code that were asked during a full-stack developer interview. It involved creating a Promise, in which the candidate should implement, passing it a resolve function, ...
Erez's user avatar
  • 205
4 votes
1 answer
3k views

How to use React's getDerivedStateFromProps with a setTimeout?

I want to create a Modal-like component that receives an open/closed boolean as a prop, then stores that value in the component state. When closing the Modal, I want to update the close boolean prop, ...
SeanMcP's user avatar
  • 293
1 vote
0 answers
2k views

Jest not covered the function inside useEffect

I want to test a function that fetch something from server, it looks like this : import React, { useEffect } from 'react'; function Component1() { useEffect(() => { fetchSomeData(); ...
jjzjx118_2's user avatar
3 votes
3 answers
229 views

function call on object with delay

I am trying to make this code return each employees name. var company = { employees: [ { name: "doug" }, { name: "AJ" } ], ...
brff19's user avatar
  • 824
1 vote
1 answer
134 views

Why await does not wait for setTimeOut

Trying to learn async/await, The below code, awaits on getUserName() to return userName after 2s. hasUser (returns a promise) then just logs the received username. The problem is console.log(msg) ...
appu's user avatar
  • 481
0 votes
3 answers
204 views

How does execution order differ when returning a value and returning a promise in .then?

So I'm trying to grasp Promises and as of now I think I'm understanding everything I've read so far except for one example listed on MDN Web Docs (https://developer.mozilla.org/en-US/docs/Web/...
Agustin Lleras's user avatar
0 votes
2 answers
261 views

setTimeout function does not display HTML after function ends

I have created a timeout function for 3 seconds and after that time it is supposed to render the HTML. I'm following the official documentation to do this and the code is like on the documentation. ...
Clayton Crockville's user avatar
1 vote
0 answers
114 views

Showing an element for 3 seconds then remove it and show another element - jQuery

I have a small function to show some data to an user that looks like this. function message(msg) { var html = '<div class="reply" style="display: flex;"><div class=&...
lStoilov's user avatar
  • 1,329
1 vote
1 answer
55 views

Get value from setTimout inside a forEach

I have now spent several hours here reading through the solutions to this problem, like this: Get return value from setTimeout But I can't find any solution for my problem to get the removeCount value....
edass's user avatar
  • 45