writable.destroy([error])
曆史
版本 | 變化 |
---|---|
v14.0.0 | 在已被銷毀的流上作為 no-op 工作。 |
v8.0.0 | 添加於:v8.0.0 |
參數
銷毀流。可選擇發出 'error'
事件,並發出 'close'
事件(除非 emitClose
設置為 false
)。在此調用之後,可寫流已結束,隨後對 write()
或 end()
的調用將導致 ERR_STREAM_DESTROYED
錯誤。這是一種破壞性的直接破壞流的方法。以前對 write()
的調用可能尚未耗盡,並且可能會觸發 ERR_STREAM_DESTROYED
錯誤。如果數據應該在關閉之前刷新,則使用end()
而不是destroy,或者在銷毀流之前等待'drain'
事件。
const { Writable } = require('node:stream');
const myStream = new Writable();
const fooErr = new Error('foo error');
myStream.destroy(fooErr);
myStream.on('error', (fooErr) => console.error(fooErr.message)); // foo error
const { Writable } = require('node:stream');
const myStream = new Writable();
myStream.destroy();
myStream.on('error', function wontHappen() {});
const { Writable } = require('node:stream');
const myStream = new Writable();
myStream.destroy();
myStream.write('foo', (error) => console.error(error.code));
// ERR_STREAM_DESTROYED
一旦 destroy()
被調用,任何進一步的調用都將是 no-op 並且除了來自 _destroy()
之外的任何其他錯誤都不會作為 'error'
發出。
實施者不應覆蓋此方法,而應實施
。writable._destroy()
相關用法
- Node.js stream.Writable.destroyed用法及代碼示例
- Node.js stream.Writable.uncork()用法及代碼示例
- Node.js stream.Writable.end([chunk[, encoding]][, callback])用法及代碼示例
- Node.js stream.Writable.write(chunk[, encoding][, callback])用法及代碼示例
- Node.js stream.Readable.take(limit[, options])用法及代碼示例
- Node.js stream.Readable.pipe(destination[, options])用法及代碼示例
- Node.js stream.Readable.setEncoding(encoding)用法及代碼示例
- Node.js stream.Readable.some(fn[, options])用法及代碼示例
- Node.js stream.Readable.map(fn[, options])用法及代碼示例
- Node.js stream.Readable.toArray([options])用法及代碼示例
- Node.js stream.Readable.isPaused()用法及代碼示例
- Node.js stream.Readable.forEach(fn[, options])用法及代碼示例
- Node.js stream.Readable.every(fn[, options])用法及代碼示例
- Node.js stream.finished()用法及代碼示例
- Node.js stream.Readable.from()用法及代碼示例
- Node.js stream.Readable.read([size])用法及代碼示例
- Node.js stream.Readable.flatMap(fn[, options])用法及代碼示例
- Node.js stream.finished(stream[, options], callback)用法及代碼示例
- Node.js stream.Readable.unshift(chunk[, encoding])用法及代碼示例
- Node.js stream.Readable.filter(fn[, options])用法及代碼示例
- Node.js stream.Readable.asIndexedPairs([options])用法及代碼示例
- Node.js stream.Readable.drop(limit[, options])用法及代碼示例
- Node.js stream.Readable.resume()用法及代碼示例
- Node.js stream.Readable.reduce(fn[, initial[, options]])用法及代碼示例
- Node.js stream.addAbortSignal(signal, stream)用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 stream.Writable.destroy([error])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。