@bitty/insist
Version:
Insistently runs a callback and only resolves the promise when its result is truthy.
1 lines • 2.31 kB
Source Map (JSON)
{"version":3,"file":"insist.mjs","sources":["../src/insist.js"],"sourcesContent":["/**\n * Waits the received time in milliseconds.\n * @param time - The delay time in milliseconds.\n */\nfunction wait(time) {\n return new Promise(function (resolve) { return setTimeout(resolve, time); });\n}\n/**\n * Runs condition asynchronously. Assumes `false` if condition throws an error.\n * @param condition - The condition function.\n */\nfunction check(condition) {\n try {\n return Promise.resolve(condition()).catch(function () { return false; });\n }\n catch (_a) {\n return Promise.resolve(false);\n }\n}\n/**\n * Insists on a condition until it is satisfied. When the condition is not\n * satisfied there is a delay, defined in milliseconds, for it to be checked\n * again.\n * @example\n * ```js\n * insist(() => window.document.readyState === 'complete').then(() => {\n * console.log('The DOM is ready!');\n * });\n * ```\n * @param condition - The condition function.\n * @param time - The delay time, in milliseconds, to execute condition again.\n */\nfunction insist(condition, time) {\n if (time === void 0) { time = 200; }\n return check(condition).then(function (success) {\n if (success)\n return;\n return wait(time).then(function () { return insist(condition, time); });\n });\n}\nexport default insist;\n//# sourceMappingURL=insist.js.map"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA,SAAS,IAAI,CAAC,IAAI,EAAE;AACpB,IAAI,OAAO,IAAI,OAAO,CAAC,UAAU,OAAO,EAAE,EAAE,OAAO,UAAU,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;AACjF,CAAC;AACD;AACA;AACA;AACA;AACA,SAAS,KAAK,CAAC,SAAS,EAAE;AAC1B,IAAI,IAAI;AACR,QAAQ,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC,YAAY,EAAE,OAAO,KAAK,CAAC,EAAE,CAAC,CAAC;AACjF,KAAK;AACL,IAAI,OAAO,EAAE,EAAE;AACf,QAAQ,OAAO,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AACtC,KAAK;AACL,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE;AACjC,IAAI,IAAI,IAAI,KAAK,KAAK,CAAC,EAAE,EAAE,IAAI,GAAG,GAAG,CAAC,EAAE;AACxC,IAAI,OAAO,KAAK,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,UAAU,OAAO,EAAE;AACpD,QAAQ,IAAI,OAAO;AACnB,YAAY,OAAO;AACnB,QAAQ,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE,OAAO,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;AAChF,KAAK,CAAC,CAAC;AACP;;;;"}