Replies: 0 comments 5 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')) |
1 reply
|
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() |
0 replies
|
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 |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
What is the expected output?
Content of the file referenced by the first promise (
existingFilePromise) logged,ENOENTerror 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.
All reactions