2016-12-08 103 views
1

我有以下代码:调用解析功能发生在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跟踪。现在更困惑:

enter image description here

+0

你在期待'this'是中前' Promise'解析器功能? – guest271314

+0

你可以把一个控制台.log前都拒绝调用 - 只是为了调试 –

回答

4
xhr.onreadystatechange = function() { 
    if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) { 
     console.log(xhr.response); 
     resolve(xhr.response); 
    }else { 
     reject(); 
    } 
} 

有关onreadystatechange的事情是,它被称为多次

有关承诺解决的事情是,一旦拒绝或解决的,它不能被拒绝或再次来拆分

第一次的onreadystatechange被调用时,状态会是1,而不是4 ...所以你拒绝

如果状态是,你应该只拒绝4和(完成)和状态!= 200

xhr.onreadystatechange = function() { 
    if (xhr.readyState == XMLHttpRequest.DONE) { 
     if(xhr.status == 200) { 
      console.log(xhr.response); 
      resolve(xhr.response); 
     } else { 
      reject(); 
     } 
    } 
} 

另外,使用的onload/onError的 - 尽管你仍然需要在onload事件

xhr.onload= function() { 
    if(xhr.status == 200) { 
     console.log(xhr.response); 
     resolve(xhr.response); 
    } else { 
     reject(); 
    } 
} 


xhr.onerror= function() { 
    reject(); 
} 

检查状态== 200有关的东西旁注的作品,但看起来错

return new Promise (function (resolve,reject) { 
    if (this.ImageData && averageColor) { 

this诺言构造回调里面可能是window,或者甚至甚至可以undefined严格模式 - 你需要修复这些代码是导致问题走下赛场

+0

用'readystatechange'事件代替'load'事件应该可以解决问题,是吗? – guest271314

+0

是的,但是然后'onerror'也应该被处理 - 并且'onload'仍然需要检查统计信息== 200 –

+0

你不会在'Promise'构造函数 – guest271314

0

thisPromise构造为window,除非专门设置为不同的值。 this.ImageData将评估为false而不将this设置为具有.ImageData作为致电new Promise()的属性的对象。

替代引用对象具有属性ImageData对于this.ImageDataPromise构造函数解析器函数。

例如

function resolver(resolve, reject) { 

} 

new Promise(resolver.bind(/*object that has `.ImageData` as property*/)) 

此外,替代load事件readystatechange事件,并且包括用于XMLHttpRequest对象error事件处理程序。

+0

谢谢你。这并不能解决问题。它正在调用onreadystatechange函数中的解析函数(我可以通过console.log看到)。然而,它现在已经出现在了。然后 – fur866

+0

@ fur866 _“但是,它现在显示在.then”_不确定你的意思? – guest271314

+0

事实上'console.log(xhr.response);'被输出意味着(但是被误导)如果条件为TRUE - 所以应该没有拒绝被称为 –

1

您遇到此问题的原因是onreadystatechange在请求完成之前以及在完成之前被调用多次,您将得到您的else条件,该条件调用reject。您只应拒绝,如果DONExhr.status != 200。这里有一个例子:

if (xhr.readyState == XMLHttpRequest.DONE) { 
    if (xhr.status == 200) { 
     resolve(xhr.response); 
    } else { 
     reject(); 
    } 
}