I have a function similar to the code below - it makes a fetch
, does things when the call is successful or not, and returns a promise to be handled downstream:
const myFunc = (): Response => {
return fetch('http://something')
.then(r => {
console.log('the call went through, the return code does not matter')
return r
})
.catch(err => {
console.error(`ah crap, the call failed completely: ${err}`)
// and here I do not know how to return a new Response
}
}
}
myFunc().then(r => {
console.log('do something once myFunc is over')
})
My intention is to run some code once myFunc
is done, but I need to account for the case the call completely fails (no DNS resolution, rejected call, ...). For that I need to create and return in the catch()
a Response
promise with, for instance, a forced 503
return code (which is not really correct because the server did not even answer, but anyway).
My question: how can I create such a Response
promise?