1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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);
- });
- }
- export function objectForEach<O extends Record<PropertyKey, unknown>>(
- obj: O,
- callback: (value: O[keyof O], key: PropertyKey) => void
- ) {
- const keys = Reflect.ownKeys(obj);
- keys.forEach((key) => {
- callback(Reflect.get(obj, key), key);
- });
- }
- export async function asyncObjectForEach<
- O extends Record<PropertyKey, unknown>
- >(
- obj: O,
- callback: (value: O[keyof O], key: PropertyKey) => boolean | Promise<boolean>
- ) {
- const keys = Reflect.ownKeys(obj);
- let index = 0;
- const newCallback = async () => {
- const key = keys[index];
- const res = await callback(Reflect.get(obj, key), key);
- if (index < keys.length - 1 && res !== false) {
- index++;
- await newCallback();
- }
- };
- return await newCallback();
- }
|