TS1103
error TS1103: ‘for await’ loops are only allowed within async functions and at the top levels of modules.
Broken Code ❌
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import fs from 'node:fs'; import readline from 'node:readline';
const inputStream = fs.createReadStream('file.csv');
function printLine(file: string): Promise<void> { const rl = readline.createInterface({ input: inputStream, crlfDelay: Infinity, });
for await (const line of rl) { console.log(line); } }
|
Fixed Code ✔️
You have to mark your function with the async
keyword:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import fs from 'node:fs'; import readline from 'node:readline';
const inputStream = fs.createReadStream('file.csv');
async function printLine(file: string): Promise<void> { const rl = readline.createInterface({ input: inputStream, crlfDelay: Infinity, });
for await (const line of rl) { console.log(line); } }
|