Node.js/stream/柯里化并发:修订间差异
来自康健生活
创建页面,内容为“<syntaxhighlight lang="javascript"> // 公共函数柯里化方法 let currying = function (fun, arr = []) { // 取出参数 let args = Array.prototype.slice.call(…” |
无编辑摘要 |
||
| 第33行: | 第33行: | ||
}) | }) | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
柯里化读取流函数,并发读取后,写入数据流 | |||
<syntaxhighlight lang="javascript"> | |||
// 柯里化函数 | |||
let _getFile = currying(getFile) // 函数提升 | |||
// 写入数据流 | |||
_getFile('first.txt')('second.txt')('three.txt')() | |||
.then(res => { | |||
if (!res.length) return | |||
// 创建fs写入数据流(output.txt) | |||
let writerStream = fs.createWriteStream('output.txt') | |||
// 遍历读取的结果并且写入到output.txt | |||
res.forEach(item => writerStream.write(item, 'UTF8')) | |||
// 写入结果 | |||
writerStream.end(() => { | |||
// 输出文件内容 | |||
fs.readFile('output.txt', (err,data) => { | |||
console.log('并发写入成功') | |||
console.log(data.toString()) // 隐式类型转换 | |||
}) | |||
}) | |||
})</syntaxhighlight> | |||