J
J
jenya77712018-02-08 23:02:06
Node.js
jenya7771, 2018-02-08 23:02:06

How to set the maximum time for a function to run?

hello there is a function

function reconnection() {
//........
    setTimeout(reconnection, 3000)
}

How to set the time after which it should restart if it works too long?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2018-02-09
@RidgeA

Something like this

const sleep = timeout => new Promise(resolve => setTimeout(resolve, timeout));
const result = Promise.race([
  sleep(1000).then(() => throw new TimeoutError()),
  getSomePromiseThatShouldWorkNoLongerThanOneSecond()
]).then(...).catch(...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question