Skip to content

callWithSignal

Functions

callWithSignal()

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

支持通过 AbortSignal 控制函数终止

Type parameters

ParameterValue
T extends AnyFnAnyFn

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

Parameter
T extends AnyFn

Properties

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