Night owl
Night owl

Reputation: 145

Can any JS expert look at the my code and told what i am doing wrong

Can any one help me to understand the following code. i am creating the folder using the mkdirp-promise in the nodejs . what the difference between the followig code

1) mkdirp('/hello2')
    .then(console.log("hello")) 
    .catch(console.error)

2)    mkdirp("/test").then(() => {
    console.log("directory made");
    console.log("hello");
    }).catch(console.error);

1) when i run the first code i get the hello inside the console and also the hello2 folder is created 2) But when i run the second one the folder test is created but i did't get the log why ?? i have to use the 2nd case

Upvotes: 0

Views: 61

Answers (1)

dev
dev

Reputation: 841

You should consider using async/await to make your code cleaner.

try {
 await mkdirp('/hello2');
 console.log('hello');
 await mkdirp("/test");
 console.log("directory made");
} catch (error) {
 console.error(error);
}

or else

mkdirp('/hello2')
 .then(() => {
   console.log('hello');
   return mkdirp("/test");
 })
 .then(() => {
   console.log("directory made");
 })
 .catch(console.error);

Upvotes: 1

Related Questions