ThomasReggi
ThomasReggi

Reputation: 59355

Adding timeout to promise that hangs

Im using a promise for a child process that does not have it's own timeout. I've tried Promise Bluebird's race method, it is throwing but still hanging the console.

const done = () => Promise.delay(500).then(() => throw new Error('timeout')
const fire = () => Promise.race([promiseHangs(url), done()])
fire().then(console.log)

How can I resolve the promiseHangs promise and stop the async process from running?

Upvotes: 1

Views: 154

Answers (1)

Julia Finarovsky
Julia Finarovsky

Reputation: 21

The result of const done = () => Promise.delay(500).then(() => throw new Error('timeout') is a thrown error. You are better off returning an actually promise from that using Promise.reject('timeout')

According to the documentation, the Promise.any method will return the first result and won't allow a rejected value you to win.

So const fire = () => Promise.any([promiseHangs(url), done()]) should work.

Upvotes: 1

Related Questions