1

I have below piece of code in my nodejs. Here I have defined screenshot as const and then redefine it as you can see below. Here the issue is my code was neither throwing any error nor being executed further after the console console.log('opopopopopoopoooooopop'). After sometime I figured out the reason is "I have taken `screenshot variable as const".

const handler = async(request, reply) => {
  try {
    const screenshotPath = ''
    console.log(request.payload, 'ddddddddddddddddddd')
    let folderName = `./public/applications/${applicationId}`
    let filepath = `${folderName}/${className}.png`
    mkdirp(folderName, async(err) => {
      await imageUpload(file, className, filepath)
      console.log('oooooooooooooooooooo')
      if (err) {
        return reply({ success: false, message: err.message, data: null })
      }
      console.log('opopopopopoopoooooopop')
      screenshotPath = filepath
      console.log(Event)
      const screen = await Event.findOne({ })
      console.log(screen, 'popopopopopoopopooop')
    })
  } catch (err) {
    console.log({ err })
    return reply({ success: false, message: err.message, data: null })
  }
}

But the problem is why my code was not throwing the error here. Can someone please help me to get understand this.

Thank you!!!

5
  • Possible duplicate of Is it possible to catch exceptions thrown in a JavaScript async callback? Commented Mar 26, 2019 at 12:24
  • did you try to add try-catch block in inner async function? Commented Mar 26, 2019 at 12:27
  • @JamesDeSouza no... can single try catch cannot do this? Commented Mar 26, 2019 at 12:29
  • apparently not! when you run async method, it's work like another thread. not something to catch with. Commented Mar 26, 2019 at 12:33
  • @JamesDeSouza So is there any way to handle with single catch block? Commented Mar 26, 2019 at 12:38

1 Answer 1

2

An error within an async function causes the returned Promise to reject.

...but if nothing is using that Promise:

const handler = async () => {
  try {
    const screenshotPath = '';
    const callback = async () => {
      screenshotPath = 'redefined'; 
    }
    const promise = callback();  // <= promise will reject...
  } catch (err) {
    console.log('in catch');  // ...but this never runs
  }
  console.log('finished');
}

handler();

...then nothing happens.

This is called an unhandled promise rejection.


On the other hand, if the same Promise is await-ed, then the catch will get called:

const handler = async () => {
  try {
    const screenshotPath = '';
    const callback = async () => {
      screenshotPath = 'redefined'; 
    }
    await callback();
  } catch (err) {
    console.log('in catch');  // this runs!
  }
  console.log('finished');
}

handler();


So since the error happens in an async callback, the Promise it returns will reject, but since nothing is using that rejected Promise nothing happens.


Solution

It looks like your mkdirp function follows...

the common error-first callback style, i.e. taking an (err, value) => ... callback as the last argument

...so you could use Node's util.promisify to create a version of mkdirp that returns a Promise:

const util = require('util');
const mkdirpPromisified = util.promisify(mkdirp);

const handler = async(request, reply) => {
  try {
    let screenshotPath = ''
    let folderName = `./public/applications/${applicationId}`
    let filepath = `${folderName}/${className}.png`
    await mkdirpPromisified(folderName)
    await imageUpload(file, className, filepath)
    if (err) {
      return reply({ success: false, message: err.message, data: null })
    }
    screenshotPath = filepath
    const screen = await Event.findOne({ })
  } catch (err) {
    console.log({ err })
    return reply({ success: false, message: err.message, data: null })
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.