123456789101112131415161718192021222324252627282930313233343536373839 |
- export async function asyncWhile(
- checker: (index: number) => boolean | Promise<boolean>,
- callback: (index: number) => void | Promise<void>
- ) {
- let index = 0;
- const newCallback = async () => {
- const checkRes = await checker(index);
- if (checkRes) {
- await callback(index);
- index++;
- await newCallback();
- }
- };
- return await newCallback();
- }
- export async function asyncForEach<T = any>(
- array: T[],
- callback: (item: T, index: number) => boolean | Promise<boolean>
- ) {
- let index = 0;
- const newCallback = async () => {
- const item = array[index];
- const res = await callback(item, index);
- if (index < array.length - 1 && res !== false) {
- index++;
- await newCallback();
- }
- };
- return await newCallback();
- }
- export function nextTick(time = 0) {
- return new Promise((resolve) => {
- setTimeout(() => {
- resolve(true);
- }, time);
- });
- }
|