I have an async function that might fails:
const f = () => new Promise((_, reject) => reject("Ups"));
and a FALLBACK value constant:
const FALLBACK = { name: "Raul" };
I am trying to destructure the name field from the f() method response data, (as, if it doesn't fail, the return will be an object with this field), and in case that it fails, I use the fallback value.
I am doing the following:
function f() {
return new Promise((_, r) => r("Ups"));
}
const FALLBACK = { name: "Raul" };
(async () => {
const { name } = (await f().catch(() => {})) ?? FALLBACK; // I need to refactor this line
console.log(name);
})();
How can I refactor this code?