Loading...
function load(key) {
return Promise.try(readCache(key))
.catch(() => null);
}function load(key) {
return Promise.try(readCache, key)
.catch(() => null);
}Passing the function and its argument lets Promise.try() call it, so a sync throw becomes a rejection handled by catch().
Promise.try(fn), not its result, Promise.try(fn()).Promise.try(fn, a, b).Promise.try() replaces wrappers like new Promise((r) => r(fn())).Promise.try(fn) and Promise.resolve().then(fn)?Promise.try() calls the function right away, synchronously. .then(fn) delays the call to a microtask, which changes execution order.
Promise.try() the same as an async function?Close: both turn sync errors into rejections. Promise.try() does it inline for an existing function, without writing an async wrapper.