-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistency: runtime.onMessage Promise return #338
Comments
I think I came across this in Firefox too. I had to implement using |
I'm supportive of this in Chrome (though it's not yet implemented). To clarify, returning a Promise from a message listener would keep the message pipe open until the promise is resolved, at which point it passes the resolved value back to the caller. One note we might need to talk more about is if the Promise rejects. |
If returning promises in Chrome is a breaking change, accepting promises in chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
sendResponse(asyncFunction())
}); I'd love to see this soon, especially with rejection propagation across contexts.
Rejection propagation currently requires manual error serialization and a setup like: import {serializeError} from 'serialize-error';
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
asyncFunction().then(sendResponse, error => {
sendResponse({$$error: serializeError(error)});
throw error;
});
return true;
}); import {deserializeError} from 'serialize-error';
const response = await chrome.runtime.sendMessage('my message')
if (response.$$error) {
// Preserves both stacks
throw new Error('Response error', {
cause: deserializeError(response.$$error);
});
}
console.log(response) |
@fregante Can you expand on this Safari issue? |
I only partially tested Safari, but the repro should be: // background.js
chrome.runtime.onMessage.addListener(async () => {
throw new TypeError('catch me');
}); // contentscript.js
chrome.runtime.sendMessage('hi');
// => Promise<undefined> I'd expect Mozilla's polyfill instead re-throws the error as expected: |
Chrome MV3
runtime.onMessage
returns immediately instead of waiting for the promise to resolve.popup.js (browser action)
background.js (service worker)
Result
See also:
Issue 1185241: Support Promise as return value from runtime.onMessage callback
Chrome mv3 await sendMessage to background service worker
The text was updated successfully, but these errors were encountered: