callforth
Version:
A tiny utility library to conveniently replace callbacks with Promises.
3 lines (2 loc) • 721 B
JavaScript
var e=function(e){return new Promise(function(r){return setTimeout(r,e)})};exports.eventOn=function(e,r,n){var t,i;void 0===n&&(n="error");var o=new Promise(function(e,r){t=e,i=r});return e.addEventListener(r,t),e.addEventListener(n,i),o.finally(function(){e.removeEventListener(r,t),e.removeEventListener(n,i)}),o},exports.polling=function r(n,t){void 0===t&&(t={});try{var i=t.maxTries,o=void 0===i?10:i,v=t.interval,s=void 0===v?10:v;return Promise.resolve(function(){if(o<=0)throw void 0;return n()?void 0:Promise.resolve(e(s)).then(function(){return Promise.resolve(r(n,{maxTries:o-1,interval:s})).then(function(){})})}())}catch(e){return Promise.reject(e)}},exports.timeout=e;
//# sourceMappingURL=callforth.js.map