Marek M
Marek M

Reputation: 87

Why doesn't this try and catch work in this case?

I'm learning Node.js but cannot understand why this is not working. What is the problem? Thanks for an answer.

const fs = require('fs')
const path = require('path')

try {
    fs.mkdir(path.join(__dirname, '/test'), {}, err => {
    console.log(err)
    if (err) throw err
    })
}
catch (err) {
    console.log('file is already created')
}

This is the result:

Error: EEXIST: file already exists, mkdir 'c:\Users\stefa\Desktop\programming\learning-node\playground\paths\test'

Upvotes: 1

Views: 190

Answers (2)

AgainPsychoX
AgainPsychoX

Reputation: 1791

Callback created by lambda expression of err => {/**/} is ran asynchronously. try-catch cannot catch that.

You should use Promises/Futures or synchronized version of fs.mkdir function, fs.mkdirSync.

Try something like this:

const fs = require('fs')
const path = require('path')

try {
    fs.mkdirSync(path.join(__dirname, '/test'));
}
catch (err) {
    console.log('file is already created')
}

or

const fs = require('fs');
const path = require('path');
const util = require('util');
const mkdirPromisifed = util.promisify(fs.mkdir);

(async() {
    // ... all other code also should be written in async manner ...
    try {
        await mkdirPromisifed(path.join(__dirname, '/test'));
    }
    catch (err) {
        console.log('file is already created')
    }
}());

Upvotes: 4

T.J. Crowder
T.J. Crowder

Reputation: 1073968

As PsychoX said, the callback is called asynchronously.

You have a few choices:

  1. Just use the callback

  2. Use the promises-based fs API (or use util.promisify on mkdir, but...)

  3. (Not recommended) Use the Sync versoin of mkdir (mkdirSync)

Here's #1:

const fs = require('fs')
const path = require('path')

fs.mkdir(path.join(__dirname, '/test'), {}, err => {
    if (err) {
       console.log('file is already created')
       return
    }
    // Do the next thing here
})

Here's #2, using fsPromises.mkdir:

const fsp = require('fs').promises
const path = require('path')

fsp.mkdir(path.join(__dirname, '/test'))
.then(() => {
    // Do the next thing here
})
.catch(err => {
   console.log('file is already created')
})

or within an async function:

try {
    await fsp.mkdir(path.join(__dirname, '/test'))
    // Do the next thing here
} catch (err) {
   console.log('file is already created')
}

Upvotes: 3

Related Questions