mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-09 23:10:20 +08:00
feat(abortController): 添加readyToAbort函数用于创建并注册AbortController
提供便捷方法创建AbortController并自动注册到全局映射中,简化取消操作的流程
This commit is contained in:
parent
f76952b0dd
commit
e653a52265
@ -49,3 +49,25 @@ export function createAbortPromise<T>(signal: AbortSignal, finallyPromise: Promi
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个新的 AbortController 并将其注册到全局的 abort 映射中
|
||||||
|
* @param key - 用于标识此 AbortController 的唯一键值
|
||||||
|
* @returns AbortSignal - 返回 AbortController 的信号
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* const signal = readyToAbort('uniqueKey');
|
||||||
|
* fetch('https://api.example.com/data', { signal })
|
||||||
|
* .then(response => response.json())
|
||||||
|
* .catch(error => {
|
||||||
|
* if (error.name === 'AbortError') {
|
||||||
|
* console.log('Fetch aborted');
|
||||||
|
* }
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export function readyToAbort(key: string) {
|
||||||
|
const controller = new AbortController()
|
||||||
|
addAbortController(key, controller.abort)
|
||||||
|
return controller.signal
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user