-1

I would like to resolve a promise (the code is below) and assign the result to the qrCode variable.

const qrCode: string = await ... ?; // some async method here...?
QRScanner.prepare((err: QRScannerError, status: QRScannerStatus) => {
                    QRScanner.scan((err: QRScannerError, value: string) => {
                           Promise.resolve(value); // return value to qrCode variable...
                        }
                    });
                }
            });

How can i modify that code to get from promise a value into a qrCode variable?

str1ct
  • 33
  • 1
  • 6

1 Answers1

0

If you can, change the qrCode variable from a constant to global like this:

global qrCode; // and then....

function returned(){
 return QRScanner.prepare((err: QRScannerError, status:QRScannerStatus) => {
    QRScanner.scan((err:QRScannerError, value: string) => {
        return value;
        });
      }).promise();
}           

// assign value to qrCode
returned().then(function(value){
    qrCode = value;
});

// do something with qrCode if it is defined! E.g

if(typeof qrCode !== 'undefined'){
// user qrCode here
}

// Please note that await works only inside async functions see usage here

I hope this helps..

Nicholas Mberev
  • 1,563
  • 1
  • 14
  • 14