Skip to content

@pmun/utils / sequential

Function: sequential()

ts
function sequential<T>(tasks): Promise<T[]>;

顺序执行多个 Promise(一个接一个)

Type Parameters

T

T

Parameters

tasks

() => Promise<T>[]

Promise 工厂函数数组

Returns

Promise<T[]>

返回所有 Promise 的结果数组

Example

ts
const tasks = [
  () => fetch('/api/1').then(r => r.json()),
  () => fetch('/api/2').then(r => r.json()),
  () => fetch('/api/3').then(r => r.json()),
]

// 按顺序执行,每个完成后才执行下一个
const results = await sequential(tasks)
console.log(results)