Replies: 3 comments 2 replies
-
From my understanding, it is not sharing the same state but you won't be able to catch when import fs from 'fs'
async function readFiles() {
// we assume the AsyncIO is fast enough to finish immediately
// if the system resources is contrainted
// the behavior maybe difference
// since the Promise will be finish in next tick
const existingFilePromise = fs.promises.readFile('./exists') // mircrotask - ['Promise.resolve']
const notExistingFilePromise = fs.promises.readFile('./not-exists') // microtask ['Promise.resolve', 'Promise.reject']
// The await here will start clearing the mircrotask
// microtask - ['Promise.reject']
// and it still clearing the microtask
// microtask - []
// boom - unhandledRejection
const data = await existingFilePromise
console.log(data.toString('utf-8'))
try {
await notExistingFilePromise
} catch (err) {
console.error(err)
}
}
readFiles().finally(() => console.log('done')) |
Beta Was this translation helpful? Give feedback.
-
I'll convert this to a discussion because this seems to be misunderstanding rather than an actual bug. The Reduced test case: async function main() {
const p0 = new Promise((k, _) => setTimeout(() => k("ok"), 2))
const p1 = new Promise((_, k) => setTimeout(() => k("err"), 1))
await p0 // ERR_UNHANDLED_REJECTION from p1
try { await p1 } catch (e) { console.log(e) } // never reached
main() Instead do something like this: async function main() {
const p0 = new Promise((k, _) => setTimeout(() => k("ok"), 2))
const p1 = new Promise((_, k) => setTimeout(() => k("err"), 1))
const p2 = Promise.all([p0, p1])
try { await p2 } catch (e) { console.log(e) } // logs "err"
}
main() |
Beta Was this translation helpful? Give feedback.
-
Actually got the answer here: https://stackoverflow.com/questions/75164302 - it is a Node only behaviour which is configurable. On browser engines like Chromium it works as I expected and after reconfigured |
Beta Was this translation helpful? Give feedback.
-
What is the expected output?
Content of the file referenced by the first promise (
existingFilePromise
) logged,ENOENT
error logged for the second promise.What do you see instead?
The following error is emitted when awaiting
existingFilePromise
:Apparently the two parallel reads share state, they fail together instead of being isolated and handled separately.
Beta Was this translation helpful? Give feedback.
All reactions