Skip to content

Commit 3328868

Browse files
committed
Converted example to ReScript and added NodeJs module accessors (sikanhe/reason-nodejs#43)
1 parent c783e1c commit 3328868

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

examples/StreamTextToFile.re

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/StreamTextToFile.res

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Normally `open NodeJs` would be used to shorten the module accessors
2+
// this is not done in the example to make it clear where things come from
3+
4+
let data = "Sample text to write to a file!"->NodeJs.Buffer.fromString
5+
let process = NodeJs.Process.process
6+
7+
let outputPath = NodeJs.Path.relative(~from=NodeJs.Process.cwd(process), ~to_="example__output.txt")
8+
9+
let writeStream = NodeJs.Fs.createWriteStream(outputPath)
10+
11+
let logErrorIfExists = maybeError =>
12+
switch Js.Nullable.toOption(maybeError) {
13+
| Some(err) => Js.log2("An error occurred", err)
14+
| None => ()
15+
}
16+
17+
let () =
18+
writeStream
19+
->NodeJs.Stream.writeWith(
20+
data,
21+
~callback=maybeError => {
22+
logErrorIfExists(maybeError)
23+
Js.log("Finished")
24+
},
25+
(),
26+
)
27+
->ignore

0 commit comments

Comments
 (0)