0

Why does changing the values of status and output inside the callback of a function I await for doesn't change their values outside?

var status = '';
var output = '';

if (orderTranscription != "TRANSCRIPTION_ERROR") {

    await runPython38Script ("processing.py", orderTranscription, (output) => {

        const orderInfo = JSON.parse(output);

        // Both of these have a value defined here when I console.log, not empty string
        status = orderInfo.status ? "VALID" : "PROCESSING_ERROR";
        output = orderInfo.output;
    });
}

// When I try to console.log outside, I get empty string.
console.log(status);
console.log(output);
Filipe Aleixo
  • 3,924
  • 3
  • 41
  • 74
  • 1
    Your `await` doesn't actually do anything here: `await`ing something that isn't a Promise (N.B. AFAIK it also *must* be a native Promise, not bluebird or q) is a no-op. If there isn't a promise-returning version of your function, then you'll have to wrap it in a Promise and resolve it in the callback. – Jared Smith Mar 11 '21 at 14:08
  • are you sure ```if (orderTranscription != "TRANSCRIPTION_ERROR")``` match this condition..? – MD. RAKIB HASAN Mar 11 '21 at 14:17
  • 1
    @JaredSmith "*AFAIK it also must be a native Promise, not bluebird or q*" [you can `await` any `.then()`-able](https://jsbin.com/juyagap/1/edit?js,console), not just native promises. – VLAZ Mar 11 '21 at 14:55
  • 1
    Might be relevant: [How do I convert an existing callback API to promises?](https://stackoverflow.com/q/22519784) – VLAZ Mar 11 '21 at 15:01
  • @VLAZ I stand corrected. – Jared Smith Mar 11 '21 at 16:46

0 Answers0