c00000fd
c00000fd

Reputation: 22275

How to tell if asynchronous JavaScript function for Chrome Extension fails?

I'm trying to learn how to code Google Chrome extensions and I keep seeing that they use asynchronous JavaScript functions, as such:

chrome.storage.sync.set({'value': theValue}, function() {
    // Notify that we saved.
    message('Settings saved');
});

The function above calls a completion method upon successful result, which is good.

But what I'm curious to know is how to tell if this asynchronous function fails?

Upvotes: 0

Views: 77

Answers (1)

Xan
Xan

Reputation: 77531

The more or less uniform mechanism for Chrome APIs is to set chrome.runtime.lastError. It's usually indicated in the docs that it will be set if something goes wrong.

chrome.storage.sync.set({'value': theValue}, function() {
    if(chrome.runtime.lastError) {
        console.error(chrome.runtime.lastError.message);
    } else {
        // Notify that we saved.
        message('Settings saved');
    }
});

Upvotes: 3

Related Questions