@useorbis/db-sdk
Version:
Orbis' Typescript SDK for building open-data experiences.
34 lines (33 loc) • 863 B
JavaScript
/**
*
* Original implementation taken from Radash
* https://github.com/rayepps/radash/blob/03dd3152f560414e933cedcd3bda3c6db3e8306b/src/async.ts#L265
* Slightly modified to simplify it for our use case.
*
**/
const isFunction = (value) => {
return !!(value && value.constructor && value.call && value.apply);
};
const isPromise = (value) => {
if (!value)
return false;
if (!value.then)
return false;
if (!isFunction(value.then))
return false;
return true;
};
export const catchError = (func) => {
try {
const result = func();
if (isPromise(result)) {
return result
.then((value) => [value, undefined])
.catch((err) => [undefined, err]);
}
return [result, undefined];
}
catch (err) {
return [undefined, err];
}
};