So Deno has a filesystem watcher API that seems... obtuse to me. Perhaps that's simply due to my ignorance of async iterators. It's shown to be used like so:
const watcher = Deno.watchFs("/directory")
for await (const event of watcher) {
//process events
}
//code here is never executed
watcher
is an async iterable which is infinite, which means this for loop will block forever. What is a sensible way to write code to process these events such that it will not block the function?
Aside: why on earth did they not simply have a callback / event listener style interface?
Don't have anything after the loop, other than code you want to run after the FsWatcher
is closed.
async function watchFs(){
const watcher = Deno.watchFs("/directory")
for await (const event of watcher) {
//process events
}
// some finalisation
}
const watcher = watchFs().catch(handleSomething)
moreThings()
It's a different way of displaying the same concept. I wouldn't argue that async
/await
"is a bad design" when it's doing what was intended of showing you the flow of an asynchronous program in a sequential view.
async function watchFs(){
const stream = stream.watchFs("/directory")
stream.on('change', (event) => {
// process events
})
stream.on('end', () => {
// some finalisation
}
stream.on('error', (error) => {
handleSomething()
})
return stream
}
const watcher = watchFs()