我正在尝试学习如何对Google Chrome扩展程序进行编码,并且我一直看到他们使用异步JavaScript函数as such:
chrome.storage.sync.set({'value': theValue}, function() {
// Notify that we saved.
message('Settings saved');
});
上述函数在成功结果时调用完成方法,这很好。
但我很想知道如何判断这个异步函数是否失败?
答案 0 :(得分:3)
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');
}
});