Node.js how to .then after declaring a variable as a function that has a promise inside

121 Views Asked by At

I am attempting to set a variable scannedCode to a scanned qr code result. However, the steps after that happens continues even before scannedCode is in use, due to the promises. How can I make it so nothing continues until the variable finishes and is usable?

function stuff(image) {
      var scannedCode = qrRun(image);
     console.log("I want to console.log scannedcode and it be the actual thing completed")
}

async function qrRun(image) {
  const img = await jimp.read(fs.readFileSync(image));
  const qr = new QRReader();
  const value = await new Promise((resolve, reject) => {
    qr.callback = (err, v) => err != null ? reject(err) : resolve(v);
    qr.decode(img.bitmap);
  });
  console.log(value.result);
  return value.result;
}

Or if there is no way to do that, how would I just remove the Promise from the function currently there so I don't need a .then without breaking the qrrun.

1

There are 1 best solutions below

0
Evert On
async function stuff(image) {
  const scannedCode = await qrRun(image);
}