rubico
Version:
[a]synchronous functional programming
8 lines (7 loc) • 1.22 kB
JavaScript
/**
* rubico v2.6.2
* https://github.com/a-synchronous/rubico
* (c) 2019-2024 Richard Tong
* rubico may be freely distributed under the MIT license.
*/
const isPromise=r=>null!=r&&"function"==typeof r.then,promiseAll=Promise.all.bind(Promise),__=Symbol.for("placeholder"),curry3ResolveArg0=(r,t,e)=>function(n){return r(n,t,e)},curry3ResolveArg1=(r,t,e)=>function(n){return r(t,n,e)},curry3ResolveArg2=(r,t,e)=>function(n){return r(t,e,n)},curry3=function(r,t,e,n){return t==__?curry3ResolveArg0(r,e,n):e==__?curry3ResolveArg1(r,t,n):curry3ResolveArg2(r,t,e)},catcherApply=function(r,t,e){return r(t,...e)},isArray=Array.isArray,areAnyValuesPromises=function(r){if(isArray(r)){const t=r.length;let e=-1;for(;++e<t;){const t=r[e];if(isPromise(t))return!0}return!1}for(const t in r){const e=r[t];if(isPromise(e))return!0}return!1},_tryCatch=function(r,t,e){try{const n=r(...e);return isPromise(n)?n.catch(curry3(catcherApply,t,__,e)):n}catch(r){return t(r,...e)}},tryCatch=function(...r){if(r.length>2){const t=r.pop(),e=r.pop();return areAnyValuesPromises(r)?promiseAll(r).then(curry3(_tryCatch,e,t,__)):_tryCatch(e,t,r)}const t=r[0],e=r[1];return function(...r){return _tryCatch(t,e,r)}};export default tryCatch;