p-resolvify
Version:
Handle promise rejection like resolved
2 lines (1 loc) • 1.02 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).resolvify=t()}(this,(function(){"use strict";function n(n,t,e){return e?t?t(n):n:(n&&n.then||(n=Promise.resolve(n)),t?n.then(t):n)}var t=function(n){return n};function e(n,t){try{var e=n()}catch(n){return t(n)}return e&&e.then?e.then(void 0,t):e}function r(t,r){var o,u=r.handler,i=r.to;return o=function(){for(var r=arguments.length,o=new Array(r),f=0;f<r;f++)o[f]=arguments[f];return e((function(){return n(t.apply(void 0,o),(function(n){return i?[void 0,n]:n}))}),(function(n){var t=u(n);return i?[t]:t}))},function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];try{return Promise.resolve(o.apply(this,n))}catch(n){return Promise.reject(n)}}}function o(n,e){return e=Object.assign({handler:t,to:!1},e),"function"==typeof n?r(n,e):r((function(){return n}),e)()}return o.to=function(n){return o(n,{to:!0})},o}));