简体   繁体   中英

Why redefining the variable not throwing the error in async function

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!!!

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, ie 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 })
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM