var promise = new Promise(function(resolve, reject) {
//asynchronous part
var dom_domain_name_encoded;
chrome.tabs.query({
"active": true,
"currentWindow": true,
"status": "complete",
"windowType": "normal"
}, function (tabs) {
for (tab in tabs) {
dom_domain_name_encoded=tabs[tab].url;
dom_domain_name_encoded = encodeURIComponent(dom_domain_name_encoded);
console.log(dom_domain_name_encoded);
// return dom_domain_name_encoded;
}
});
//asynchronous part ends here.
if (dom_domain_name_encoded) {
console.log("encoded and returning");
resolve(dom_domain_name_encoded);
}
else {
console.log("error");
reject(Error("It broke"));
}
});
promise.then(
function(dom_domain_name_encoded) {
console.log(dom_domain_name_encoded);
}
).catch(
function(reason) {
console.log("promise not working");
}
);
我创建了一个承诺,其中执行了chrome.tabs函数。作为一个异步函数,我把它放在那里。它获取当前选项卡的url并将其传递给.then方法。现在和promises的引用一样,我希望它能运行.then方法,但是出了问题,“错误”就是我在控制台中得到的。
我做错了吗?