我正在尝试学习如何编码Google Chrome扩展程序,并且不断看到它们使用异步JavaScript函数,例如:
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');
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句