事件:'abort'
添加于:v15.0.0、v14.17.0
调用 abortController.abort()
方法时会发出 'abort'
事件。使用单个对象参数调用回调,其中单个 type
属性设置为 'abort'
:
const ac = new AbortController();
// Use either the onabort property...
ac.signal.onabort = () => console.log('aborted!');
// Or the EventTarget API...
ac.signal.addEventListener('abort', (event) => {
console.log(event.type); // Prints 'abort'
}, { once: true });
ac.abort();
与AbortSignal
关联的AbortController
只会触发一次'abort'
事件。我们建议在添加 'abort'
事件侦听器之前代码检查 abortSignal.aborted
属性是否为 false
。
任何附加到 AbortSignal
的事件侦听器都应使用 { once: true }
选项(或者,如果使用 EventEmitter
API 附加侦听器,请使用 once()
方法)以确保事件侦听器在'abort'
事件已处理。不这样做可能会导致内存泄漏。
相关用法
- Node.js ClientHttp2Session 'altsvc'事件用法及代码示例
- Node.js tls.Server 'keylog'事件用法及代码示例
- Node.js http.Server 'clientError'事件用法及代码示例
- Node.js cluste 'disconnect'事件用法及代码示例
- Node.js proces 'exit'事件用法及代码示例
- Node.js stream.Writable 'pipe'事件用法及代码示例
- Node.js stream.Readable 'end'事件用法及代码示例
- Node.js cluste 'fork'事件用法及代码示例
- Node.js stream.Writable 'unpipe'事件用法及代码示例
- Node.js Http2Session 'remoteSettings'事件用法及代码示例
- Node.js Worker 'listening'事件用法及代码示例
- Node.js tls.Server 'resumeSession'事件用法及代码示例
- Node.js InterfaceConstructor 'pause'事件用法及代码示例
- Node.js fs.FSWatcher 'change'事件用法及代码示例
- Node.js stream.Readable 'data'事件用法及代码示例
- Node.js http.ClientRequest 'connect'事件用法及代码示例
- Node.js proces 'uncaughtException'事件用法及代码示例
- Node.js Http2Session 'localSettings'事件用法及代码示例
- Node.js REPLServer 'exit'事件用法及代码示例
- Node.js cluste 'online'事件用法及代码示例
- Node.js tls.TLSSocket 'session'事件用法及代码示例
- Node.js Worker 'exit'事件用法及代码示例
- Node.js cluste 'exit'事件用法及代码示例
- Node.js Http2Stream 'trailers'事件用法及代码示例
- Node.js MessagePort 'close'事件用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 'abort'事件。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。