Skip to main content
zerotal

Documentation


Documentation / zerotal / helpers / rescueSync

Function: rescueSync()

rescueSync<T>(callback, fallback): T

Defined in: packages/core/src/helpers/index.ts:224

Synchronous sibling of rescue — for code paths that cannot await (JSON parsing, attribute decoding, hot loops). Runs callback and returns its value; on exception returns fallback (or the result of calling it with the caught error).

Type Parameters

T

T

Parameters

callback

() => T

fallback

T | ((error) => T)

Returns

T

Example

const payload = rescueSync(() => JSON.parse(raw), {});
const cursor  = rescueSync(() => JSON.parse(atob(token)), null);