I am using cordova barcode scanner and there is a sample like this.
Despite the comment there, I am unsure of why exactly does the setTimeout is necessary here.
What does it mean by so the dialog does not free the app?
cordova.plugins.barcodeScanner.scan(
    // success callback function
    function (result) {
        // wrapping in a timeout so the dialog doesn't free the app
        setTimeout(function() {
            alert("We got a barcode\n" +
                  "Result: " + result.text + "\n" +
                  "Format: " + result.format + "\n" +
                  "Cancelled: " + result.cancelled);                            
        }, 0);
    },
    // error callback function
    function (error) {
        alert("Scanning failed: " + error);
    },
    // options object
    {
        ...
    }
 
    