Skip to content
Advertisement

How do I wait for multiple promises to resolve but also include a set minumum delay?

I am calling two APIs with two different functions and having a setTimeout for both, is it possible to handle their timeouts in such a way that they finish in 10s instead of 15s.

JavaScript

Is there a way to run this code only in 10s, so that both the APIs get called in 10s time period

Advertisement

Answer

Forcing either one or both axios.get() functions to complete below some time limit (other than failing on a timeout), isn’t doable, since you don’t control the transport.

One thing you can do is force one or more functions to complete at or after some time threshold, with something like this…

JavaScript

EDIT good idea from @VLAZ to append an extra promise that forces minimal time (then slice its result off later).

The caller can say:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement