@beenotung/tslib
Version:
utils library in Typescript
32 lines (31 loc) • 710 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isPromise = isPromise;
exports.then = then;
exports.thenF = thenF;
const PromiseString = Promise.resolve().toString();
function isPromise(x) {
return String(x) === PromiseString;
}
function then(x, f, onError) {
if (isPromise(x)) {
const res = x.then(f);
if (onError) {
res.catch(onError);
}
return res;
}
return f(x);
}
function thenF(f, q, onError) {
try {
const x = f();
return then(x, q, onError);
}
catch (e) {
if (typeof onError === 'function') {
onError(e);
}
return Promise.reject(e);
}
}