writable.uncork()方法是Stream模塊的內置應用程序編程接口,用於在調用stream.cork()方法時刷新所有緩衝的數據。
用法:
writable.uncork()
參數:此方法不接受任何參數。
返回值:如果正在調用此方法,則在輸出中將再次顯示正在塞住的數據。
以下示例說明了Node.js中writable.uncork()方法的使用:
範例1:
// Node.js program to demonstrate the
// writable.uncork() method
const stream = require('stream');
// Creating a stream and creating
// a write function
const writable = new stream.Writable({
// Write function with its
// parameters
write:function(chunk, encoding, next) {
// Converting the chunk of
// data to string
console.log(chunk.toString());
next();
}
});
// Writing data
writable.write('hi');
// Calling cork() function
writable.cork();
// Again writing some data
writable.write('hello');
writable.write('world');
// Calling uncork() function
writable.uncork();
輸出:
hi hello world
在上麵的示例中,因為在此之後調用了uncork()函數,因此在輸出中還返回了已被軟化的數據。
範例2:
// Node.js program to demonstrate the
// writable.uncork() method
const stream = require('stream');
// Creating a stream and creating
// a write function
const writable = new stream.Writable({
// Write function with its
// parameters
write:function(chunk, encoding, next) {
// Converting the chunk of
// data to string
console.log(chunk.toString());
next();
}
});
// Calling cork() function
writable.cork();
// Writing data
writable.write('hi');
// Calling cork() function
writable.cork();
// Again writing some data
writable.write('hello');
// Calling uncork function
// using nextTick()
process.nextTick(() => {
// Calling uncork function
writable.uncork();
writable.uncork();
});
輸出:
hi hello
因此,您需要調用uncork()函數您已調用軟木函數的次數。在上麵的示例中,我們兩次調用了cork()函數,因此uncork函數也被調用了兩次。
參考: https://nodejs.org/api/stream.html#stream_writable_uncork
相關用法
- node.js Stream writable.end()用法及代碼示例
- node.js Stream readable.isPaused()用法及代碼示例
- node.js Stream readable.pause()用法及代碼示例
- node.js Stream writable.write()用法及代碼示例
- node.js Stream writable.cork()用法及代碼示例
- node.js Stream writable.destroy()用法及代碼示例
- node.js Stream readable.read()用法及代碼示例
- node.js Stream writable.setDefaultEncoding()用法及代碼示例
- node.js Stream readable.destroy()用法及代碼示例
- Node.js GM blur()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | Stream writable.uncork() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。