Hi I testing new "for await" feature with express.js to send data to client chunk by chunk.
Here is my first example when I getting error
const express = require('express')
const fs = require('fs')
const app = express()
app.get('/', (req, res) => {
const stream = fs.createReadStream('a.json', {
highWaterMark: 5
})
(async () => {
for await (const chunk of stream) {
res.write(`Read ${chunk}`)
}
})()
res.end('Hello World!')
})
app.listen(3000)
Here is my error
TypeError: fs.createReadStream(...) is not a function
Here is working example.
const express = require('express')
const fs = require('fs')
const app = express()
app.get('/', async (req, res) => {
const stream = fs.createReadStream('a.json', {
highWaterMark: 5
})
for await (const chunk of stream) {
res.write(`Read ${chunk}`)
}
res.send('Hello World!')
})
app.listen(3000)
Please help to understand why fs.createReadStream is not a function in first example.
Also this just not working, I don't know why.
Only prints "1" one time.
const express = require('express')
const fs = require('fs')
const app = express()
function wait(t) {
return new Promise(r => {
setTimeout(t, r);
})
}
app.get('/', async (req, res) => {
const stream = fs.createReadStream('a.json', {
highWaterMark: 5
})
for await (const chunk of stream) {
console.log(1);
await wait(500);
console.log(2);
res.write(`Read ${chunk}`);
}
res.end('Hello World!');
})
app.listen(3000)
Please help to understand why fs.createReadStream is not a function in first example.
The error message says that fs.createReadStream(...) is not a function, so it鈥檚 not about fs.createReadStream, it鈥檚 about the result of fs.createReadStream(...).
What you鈥檙e essentially writing is a piece of code that has this format:
const a = b(c)
(async () => d)()
which is parsed as const a = b(c)(async () = > d)(), because you don鈥檛 use semicolons; rather than calling b(c) and the async function, it tries to call the function b(c) with the async function as an argument.
Here and here are some more details.
tl;dr: If you don鈥檛 use semicolons, you need to prefix lines that start with any of the characters [, (, `, +, *, /, -, ,, . with a semicolon.
I鈥檓 closing this since it鈥檚 not a bug with Node.js, but if you have more question feel free to raise them at https://github.com/nodejs/help. :)
@nairihar For your second comment, the issue is that setTimeout takes its function argument as the first one, not the second one. This is different from other Node.js functions because it鈥檚 not a Node.js API. You should be seeing an error somewhere as a result of that.
Thank you @addaleax , I confused setTimeout example.
Most helpful comment
The error message says that
fs.createReadStream(...)is not a function, so it鈥檚 not aboutfs.createReadStream, it鈥檚 about the result offs.createReadStream(...).What you鈥檙e essentially writing is a piece of code that has this format:
which is parsed as
const a = b(c)(async () = > d)(), because you don鈥檛 use semicolons; rather than callingb(c)and the async function, it tries to call the functionb(c)with the async function as an argument.Here and here are some more details.
tl;dr: If you don鈥檛 use semicolons, you need to prefix lines that start with any of the characters
[,(,`,+,*,/,-,,,.with a semicolon.I鈥檓 closing this since it鈥檚 not a bug with Node.js, but if you have more question feel free to raise them at https://github.com/nodejs/help. :)