chrome.runtime.onMessage response with async await

后端 未结 4 1864
闹比i
闹比i 2021-02-05 04:03

I want to use async await in an onMessage listener:

chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) =>{
    var key = await getKey(         


        
4条回答
  •  感动是毒
    2021-02-05 04:51

    I do work-around by extracting to an async function.

    chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
      doSomethingWith(request).then(sendResponse);
      return true; // return true to indicate you want to send a response asynchronously
    });
    
    async function doSomethingWith(request) {
      var key = await getKey();
      // await .....
      return key;
    }
    

    The return value of an async function is implicitly wrapped in Promise.resolve. See async doc.

    See onMessage.

提交回复
热议问题