UNPKG

for-promise

Version:

A lightweight utility to run async loops with full control using Promises — like for, forEach, and while, but smarter.

2 lines (1 loc) 3.4 kB
var forPromise=function(){"use strict";function e(e){let t=0;return"number"!=typeof e?(r(e)||Array.isArray(e))&&(t=function(e){return Array.isArray(e)?e.length:r(e)?Object.keys(e).length:0}(e)):t=e-1,t}function t(e){const t={confirmed:!1};return r(e)&&(r(e.data)||Array.isArray(e.data)||"number"==typeof e.data)?t.confirmed=!0:"string"==typeof e.type&&"while"===e.type&&void 0!==e.while&&"function"==typeof e.checker&&(t.confirmed=!0,t.type="while"),t}function r(e){return null!==e&&"object"==typeof e&&(!Array.isArray(e)&&"[object Object]"===Object.prototype.toString.call(e))}return function(n,o){return new Promise(((l,i)=>{try{if("object"!=typeof n||null===n)throw new Error("Invalid object provided.");if("function"!=typeof o)throw new Error("Callback must be a function.")}catch(e){return i(e)}const c=t(n);if(c.confirmed){let u={error:!1,forceBreak:!1,count:0,total:null,items:[]};const a=function(e){u.error=!0,i(e)},f=function(e,t,n,o=!1){let c=null;if(c=e?d.list[t]:u,"number"!=typeof c.total)return i(new Error('Invalid "item_to_edit.total" value: null or undefined.'));const a={isObject:r(o)};if(a.isObject?"object"==typeof o&&(c.forceBreak?a.allowed=!1:a.allowed="boolean"==typeof o.break&&o.break,a.dontSendResult="boolean"==typeof o.dontSendResult&&o.dontSendResult,a.forceResult="boolean"==typeof o.forceResult&&o.forceResult):(c.forceBreak?a.allowed=!1:a.allowed="boolean"==typeof o&&o,a.dontSendResult=!1,a.forceResult=!1),(!c.error&&!c.forceBreak||a.forceResult)&&(c.count++,null!==n&&c.items.push(n),!c.forceBreak&&a.allowed&&(c.forceBreak=!0,c.count=c.total+1),!a.dontSendResult&&(c.count>=c.total||"string"==typeof c.type&&"while"===c.type&&c.count>0)))if(e)if(d.list[t]){d.list[t].complete=!0;let e=!0;for(const t in d.list)if(!d.list[t].complete){e=!1;break}e&&(u.extra=d.list,l(u))}else u.error=!0,i(new Error(`forAwait Extra Index ${t} not found.`));else d.enabled||l(u)},s=function(e,t=!1,r=-1,o=null){let l=null;t?"object"==typeof o&&(l=o):l=n;const c=function(n=null){if(u.error||u.forceBreak)return!1;try{const o=e=>f(t,r,n,e);null!==n?e(n,o,a,d.extra_function):e(o,a,d.extra_function)}catch(e){return u.error=!0,i(e),!1}return!0};if(null===l)throw new Error('Invalid "the_item" value: null or undefined.');if("number"!=typeof l.data)if(l.type)if("while"===l.type){const n=function(){if("function"!=typeof l.checker)throw new Error('Invalid "checker" function in "the_item".');if(l.checker()){let o=null;if(o=t?d.list[r]:u,"number"!=typeof o.total)throw new Error('Invalid "item_to_edit.total" value: null or undefined.');return o.total++,e((function(e){return o.count++,f(t,r,null,e)}),a,d.extra_function),n()}return f(t,r,null)};n()}else u.error=!0,i(new Error("Invalid Function Type!"));else{let e=!1;for(const t in l.data)if(e=!0,!c(t))break;if(!e)return f(t,r,null)}else{if(!(l.data>0))return f(t,r,null);for(let e=0;e<l.data&&c(e);e++);}};u.total=e(n.data),c.type&&(u.type=c.type);const d={enabled:!1,list:[],extra_function:function(r){const n=t(r);if(n.confirmed){d.enabled=!0,d.list.push({complete:!1,forceBreak:!1,count:0,total:null,items:[]});const t=d.list.length-1;return d.list[t].total=e(r.data),n.type&&(d.list[t].type=n.type),{run:function(e){u.error||u.forceBreak||s(e,!0,t,r)}}}{const e=new Error("Invalid Object Extra Type to start the Script.");return u.error=!0,i(e),null}}};s(o)}else i(new Error("Invalid Object Type to start the Script."))}))}}();