I am trying to use a Promise.race
to forcebly timeout a function
With the setup below I would expect the code below to return “value b”; the method getValues
takes more time to respond than the timeout so it should be discarded.
Running the code below I see that the function returns “value a” from the getValues
method which takes longer then the timeout.
async function getDBValues(): Promise {
let values = 'value b';
try {
values = await Promise.race([
getValues(); // This returns successfully in about 30 ms with value a
new Promise((resolve, reject) => {
setTimeout(() => reject(`rejected`), 1);
});
]);
}
catch (error) {
console.log(`Error ${error}`);
}
return values;
}
I already tried adjusting the timeouts, and using arrow functions.