/** ***************************************** * Created by edonet@163.com * Created on 2021-03-21 22:46:04 ***************************************** */ 'use strict'; /** ***************************************** * 逐步处理函数 ***************************************** */ interface Handler { (item: { index: number, value: T, next(): unknown }): void | P; } /** ***************************************** * 执行函数 ***************************************** */ function invoke(index: number, list: T[], handler: Handler): void | P { if (index < list.length) { return handler({ index, value: list[index], next: () => invoke(index + 1, list, handler) }); } } /** ***************************************** * 逐步执行 ***************************************** */ export function step(list: T[], handler: Handler): void | P { // 校验遍历列表 if (!Array.isArray(list)) { throw new Error('expect `list` to be a Array'); } // 校验执行函数 if (typeof handler !== 'function') { throw new Error('expect `handler` to be a Function'); } // 执行回调 return invoke(0, list, handler); }