我有以下代码:调用解析功能发生在JavaScript无极拒绝功能
return new Promise (function (resolve,reject) {
if (this.ImageData && averageColor) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
console.log("Image found");
resolve(xhr.response);
}else {
console.log("Image not found");
reject();
}
}
xhr.open('GET', 'http://localhost:8765/color/'+averageColor, true);
xhr.send(null);
}
else {
reject();
}
});
调用此代码如下功能:
var v = tile.getImage(tile.getColorAverage());
v.then(function (value) {
console.log("laughing");
}).catch(function() {
console.log("Catch called");
});
的问题是在我的承诺我可以看到,如果条件正确并从服务器获取响应(由于console.log)。然而,另一方面,它根本没有进入“那么”的位置(甚至没有)。由于某种原因,它会被拒绝。我疯了,因为我可以看到它执行应该解决它的位,但我没有得到任何东西。任何帮助将非常感激。
编辑:
现在我只跑了一次。这里是我的console.log跟踪。现在更困惑:
你在期待'this'是中前' Promise'解析器功能? – guest271314
你可以把一个控制台.log前都拒绝调用 - 只是为了调试 –