-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use async functions for file-system #18
Comments
Hi bro! About sync methods of fs, in this case, i think change to async doesn't make sense because next lines still must wait to previous line was done, example in if (Fs.existsSync(cacheFile)) {
data = Fs.readFileSync(cacheFile)
data = JSON.parse(data)
} else {
return fn(null, null)
}
// Next lines As you see, we must check the file is exist before we can read it, same for other lines, right? Additional, in core files, i used callback, this is a mistake 😢but it worked fine. |
Async methods don't block execution thread. Sync methods don't allow process other async operations (like http request, timers) while fs works. Example with await/async syntax: async () => {
if (await Fs.exists(cacheFile)) {
data = await Fs.readFile(cacheFile)
data = JSON.parse(data)
} else {
// do something else
}
} It is possible to improve it by modern syntax and async) easy to read and easy to use. |
Good morning bro I understood, I previously thought that the expensive task like I/O, Network,... will be executed on a worker in worker pool, not the main node event loop, so it doesn't block the main thread (event loop). I read some article about that. But by you notice, seems I have misunderstood the problem Thank you so much |
About callback, i think we will rewrite whole project to typescript as version 3, so don't care about callback syntax, lol 🤣 |
Ah, I understood, i totally wrong, the IO task still be execute on a worker, but the event loop still waiting to it result if it's synchronous. You saved my life, lol. It should be release in v2.0.8, i will working on it tonight or maybe you may help me create a PR, I grateful for that. Thank you so much |
Hi bro @FFKL, i merged and add test hook for it, but we still have a problem in fileStore constructor, we can't await constructor, so if we execute a cache method immediately after init it. Example: fileCache.init()
await fileCache.set('key', { foo: 'bar' }) // FileStore cache instance still initializing In this case, i think we should use sync method in constructor, it will not take too much cost because we just init it once. So should i change constructor to use sync method (same with old constructor)? Thank you so much |
You're right. I missed It. But sync methods in constructor it's not good decision too. 😄 I think, for initialization we should use init() method for each type of caches. Now redis cache also use async functions for auth. |
I missed redis constructor, too, lol. It's ok, we can implement separate init method for each type of caches, and it should be async. Thank bro |
Hi bro @FFKL , Thank you for your supported 😄 |
@bahung1221 cool! Good job 👍 |
Hello! Why do you use sync methods for fs? Maybe better change them to async?
The text was updated successfully, but these errors were encountered: