The short answer: there's no need to mark the Chrome storage callback async unless you want to use await
. You also don't need to mark a function that calls the Chrome storage API as async.
Examples:
// works just fine without async
chrome.storage.sync.get('someKey', function(obj) {
console.log(obj)
})
// async allows you to use await in the callback function with Promises
chrome.storage.sync.get('someKey', async function(obj) {
var response = await fetch('...');
})
// no need to make the outer `setData` function async
function setData() {
chrome.storage.sync.set({someKey: 123, function() {
// ...
})
}
Marking a function as async
is basically just to use the await
keyword within it. await
is handy because it solves "callback hell", where you have many functions that require callbacks and you have to chain them together.
Additionally, one day (see this bug) Chrome's extensions API will return Promises instead of requiring callbacks as they do today, so writing Chrome extension code will use async functions a lot more. Firefox already uses Promises in their extension API, but for backward-compatibility reasons also supports using callbacks.