stream.Readable.from()方法是Stream模塊的內置應用程序編程接口,用於從迭代器中構造Readable Streams。
用法:
stream.Readable.from( iterable, options )
參數:該方法接受上述和以下所述的兩個參數:
- iterable:它是一個實現Symbol.asyncIterator或Symbol.iterator可迭代協議的對象。
- options:這是提供給新stream.Readable([options])的選項。默認情況下,Readable.from()方法會將options.objectMode設置為true,除非未將其手動設置為false。
返回值:它返回stream.Readable。
以下示例說明了Node.js中stream.Readable.from()方法的用法:
範例1:
// Node.js program to demonstrate the
// stream.Readable.from() method
// Constructing readable from stream
const { Readable } = require('stream');
// Using async function
async function * generate() {
yield 'GfG';
yield 'CS-Portal...';
}
// Using stream.Readable.from() method
const readable = Readable.from(generate());
// Handling data event
readable.on('data', (chunk) => {
console.log(chunk);
});
console.log("Program completed!!");
輸出:
Program completed!! GfG CS-Portal...
範例2:
// Node.js program to demonstrate the
// stream.Readable.from()
// method
// Constructing readable from stream
const { Readable } = require('stream');
// Using async function
async function * generate() {
yield 'Nidhi';
yield 'GeeksforGeeks';
}
// Using stream.Readable.from() method
const readable = Readable.from(generate());
// Handling data event
readable.on('data', (chunk) => {
console.log(chunk.length);
});
console.log("Program completed!!");
輸出:
Program completed!! 5 13
參考: https://nodejs.org/api/stream.html#stream_stream_readable_from_iterable_options
相關用法
- Node.js GM whitePoint()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM threshold()用法及代碼示例
- Node.js GM thumbnail()用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js GM resize()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM charcoal()用法及代碼示例
- Node.js GM drawRectangle()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | stream.Readable.from() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。