我正在尝试学习如何编码Google Chrome扩展程序,并且不断看到它们使用异步JavaScript函数as such

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


上面的函数在结果成功时调用完成方法,这很好。

但是我很好奇的是如何知道这个异步函数是否失败?

最佳答案

Chrome API的机制大致相同,是设置chrome.runtime.lastError。通常会在文档中指出如果出现问题,将对其进行设置。

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');
    }
});

关于javascript - 如何判断Chrome扩展程序的异步JavaScript功能是否失败?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25466498/

10-12 12:58
查看更多