I want to use async await in an onMessage listener:
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) =>{
var key = await getKey(
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.