事件:'abort'


【Event: 'abort'

当调用 abortController.abort() 方法时,会触发 'abort' 事件。回调函数会使用一个包含单个 type 属性且值为 'abort' 的对象作为参数被调用:

【The 'abort' event is emitted when the abortController.abort() method is called. The callback is invoked with a single object argument with a single type property set to '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

【The AbortController with which the AbortSignal is associated will only ever trigger the 'abort' event once. We recommended that code check that the abortSignal.aborted attribute is false before adding an 'abort' event listener.】

附加到 AbortSignal 的任何事件监听器都应使用 { once: true } 选项(或者如果使用 EventEmitter API 附加监听器,则使用 once() 方法),以确保事件监听器在处理 'abort' 事件后立即被移除。否则可能会导致内存泄漏。

【Any event listeners attached to the AbortSignal should use the { once: true } option (or, if using the EventEmitter APIs to attach a listener, use the once() method) to ensure that the event listener is removed as soon as the 'abort' event is handled. Failure to do so may result in memory leaks.】