In asynchronous functions, we can simply catch the error in callback. For example :
Async func:
fs.readdir(path, function(err){
//catch error
)
As synchronous functions don't have callback, how can I catch errors?
Sync func:
fs.readdirSync(path); //throws some error
One way is to use try catch block:
try{
fs.readdirSync(path);
}
catch(err){
//do whatever with error
}
Is there any other way to do that? If yes, then which one is better?
try...catch
is the synchronous way. – Bookstack