Skip to content

Functions

callWithSignal()

callWithSignal<T>(fn, options): Promise<Awaited<ReturnType<T>>>

支持通过 AbortSignal 控制函数终止

Type Parameters

Type ParameterDefault type
T extends FnFn

Parameters

ParameterTypeDescription
fnT待执行函数
optionsCallWithSignalOptions<T>配置项

Returns

Promise<Awaited<ReturnType<T>>>

Example

ts
const fn = () => sleep(3000).then(() => 1)
const controller = new AbortController()

callWithSignal(fn, { signal: controller.signal })
  .then(console.log)
  .catch((e) => console.log('error:', e.message))

controller.abort()
// 'error: signal is aborted without reason'

Interfaces

CallWithSignalOptions<T>

Type Parameters

Type Parameter
T extends Fn

Properties

PropertyTypeDescription
payload?Parameters<T>函数载荷列表
signal?AbortSignalAbortSignal 实例