Understanding Promise-based APIs
masterThe browser namespace provides Promise-based APIs that differ from Chrome's callback-based chrome namespace in several ways:
- Return Values: Every async function returns a
Promiseinstead of accepting a callback. - Error Handling: Instead of checking
chrome.runtime.lastError, use Promise rejection handlers (.catch()) ortry/catchblocks. - Message Responses: In
onMessagelisteners, return a Promise to send a response instead of using asendResponsecallback. - Chaining: Use Promise chaining or
async/awaitto handle sequential operations instead of nested callbacks.