1

So after the CORB issue of chrome extension i migrated the api calls to background script and used callback from the background script to send response of a JSON.

So I have a doubt regarding the response i receive from callback i want it to return it.

async function request (path, apiToken, options) {
  chrome.runtime.sendMessage({type: "request", path:path, apiToken:apiToken, options:options}, (all) => {
    return Promise.resolve(all.json )
  })
}

I call request => it goes to background => background return the response => then the request function should return a promise. I'm stuck on the last step as im unable to return it from inside the callback,

Andrei Konstantinov
  • 6,971
  • 4
  • 41
  • 57
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – wOxxOm Jun 06 '19 at 12:01

1 Answers1

1

Return new Promise, resolve it inside your sendMessage callback

async function request (path, apiToken, options) {
  return new Promise(function(resolve, reject) {
      chrome.runtime.sendMessage({type: "request", path:path, apiToken:apiToken, options:options}, (all) => {
        resolve(all.json)
      })
}
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Andrei Konstantinov
  • 6,971
  • 4
  • 41
  • 57