0

For the following code, result.arr should be printed first then result.arr but its the opposite that is happening. How can i prevent this.

var ans = null;
async function getUserData() {
  await chrome.storage.local.get(["arr"], function(result) {
      console.log(result.arr);
      ans = result.arr;
    });
  console.log("qr");
}

Yes i have gone through those StackOverflow legendary answers about callbacks.

Swastik Singh
  • 181
  • 1
  • 1
  • 9
  • How is "_result.arr_" opposite to "_result.arr_" ..? – Teemu Sep 10 '20 at 15:10
  • 1
    That's not how callback-based functions should be converted to Promises or async. It's the third time you're asking the same question. – wOxxOm Sep 10 '20 at 15:11
  • 1
    I suggest using the [WebExtension polyfill](https://github.com/mozilla/webextension-polyfill) where you can use the async/await syntax easily, see examples on the linked page. – wOxxOm Sep 10 '20 at 15:12
  • Here's an example of the correct conversion: function getUserData() { return new Promise(resolve => chrome.storage.local.get("arr", r => resolve(r.arr))); } getUserData().then(data => { console.log(data) }) – wOxxOm Sep 10 '20 at 15:15

0 Answers0