【发布时间】:2018-02-01 13:16:43
【问题描述】:
我正在尝试使用 Bluebird.js 承诺 chrome.storage.sync.get。我看到 bluebird http://bluebirdjs.com/docs/working-with-callbacks.html 网站上的所有示例都使用 Promise.promisify(require(someAPI))。我试着做
const storageGet = Promise.promisify(chrome.storage.sync.get);(如果这会影响任何事情,我没有要求),然后调用
async function() {
return await storageGet('config', function (result) {
// do stuff
})
};
控制台错误是
Unhandled rejection Error: Invocation of form get(string, function, function)
doesn't match definition get(optional string or array or object keys, function
callback)
我的理解(对 Promises 和 bluebird 的理解很少)是 storageGet 被传递了错误的参数?但是 chrome.storage.sync.get 需要传递一个字符串和一个回调函数,所以我不太确定出了什么问题。此外,我可能完全不了解我对 chrome 存储的承诺。
我知道http://bluebirdjs.com/docs/api/promise.promisifyall.html#option-promisifier 有一个带有 chrome 承诺的示例,但老实说,我不太熟悉承诺以了解该示例中发生了什么。
我是否应该以某种方式承诺 Chrome 存储而不是我正在做的方式?
【问题讨论】:
标签: javascript google-chrome async-await bluebird