Closed b5414 closed 4 years ago
@b5414 await
will propagate any exceptions up, so yes you could use a try
/catch
, but you'll see exceptions thrown as if they happened inline.
The API doesn't do this - the callback fires when it's done, and you have to check runtime.lastError
to see if something went wrong. This library checks runtime.lastError
and throws it as an exception.
The API docs for chrome.storage.local.set
don't specify a return variable, and don't pass any to the callback.
The way this library works is that it converts that callback pattern to a promise that returns whatever is passed to the callback function, and in this case nothing is passed:
chrome.storage.local.set({key: value}, function(){
// value is an external variable, nothing is passed to the callback function
// Check runtime.lastError for any errors
console.log('Value is set to ' + value);
});
This would convert to:
await chrome.storage.sync.set({key: value});
console.log('Value is set to ' + value);
This isn't exactly the same, due to the errors promotion - with this library any exceptions will cause execution to stop on the line that failed, but the API example will continue through exceptions because there's no check for runtime.lastError
.
I think this is working as designed.
To answer your question:
How can I be sure, without try\catch, that everything was successful
With this library if it wasn't successful the code will stop on that line and the next line won't execute. If you want to do something specific when it fails use a try
/catch
, but best practice would be to just let the exception propagate up.
In doc:
In fact:
How can I be sure, without try\catch, that everything was successful