Skip to content

promiseWithControl

Functions

promiseWithControl()

promiseWithControl<T>(): Object

创建可控的 promise

Type parameters

Parameter
T

Returns

Object

promise

promise: Promise<T>

reject

reject: (reason?) => void

Parameters
ParameterType
reason?any
Returns

void

resolve

resolve: (value?) => void

Parameters
ParameterType
value?T | PromiseLike<T>
Returns

void

Example

ts
const { promise, resolve } = promiseWithControl()

function run() {
  // 竟态执行
  return Promise.race([promise, sleep(10e3)])
}

run()
// 中断运行,直接结束
resolve()