promise.then()不起作用吗?

时间:2015-08-13 03:58:35

标签: javascript promise

我学习了关于ecma6的承诺,我希望通过promise.then()获取json数据,但它不能通过resolve的回调工作。我在第44版的chrome中测试。 结果是enter image description here



    <button type="button" id="get">get</button>
    <script type="text/javascript">
        var ele = document.getElementById('get');
        ele.addEventListener('click', testPromise, false);
        var getData = function (url) {
            var promise1 = new Promise(function (resolve, reject) {
                var xhr = new XMLHttpRequest();
                xhr.open('GET', url);
                xhr.setRequestHeader('Accept', 'application/json');
                xhr.send();
                xhr.onreadystatechange = handler;
                xhr.responseType = 'json';
                function handler () {
                    console.log('status: ' + this.status);
                    if (this.status === 200) {
                        console.log(this.response);
                        resolve(this.response);
                    } else {
                        reject(new Error(this.statusText));
                    }
                }
            });
            return promise1;
        }

        function testPromise() {
            getData('http://1.frontendlife.sinaapp.com/test/promise.json').then(function (json) {
                console.log(json);
            }, function (error) {
                console.log(error);
            });
        }
    </script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

xhr处理程序中存在逻辑错误。在readyState === 4之前,您无法检查XHR呼叫的结果。您的问题中有一个版本,链接页面中的版本略有不同,但两者都可以在readyState === 4之前解决或拒绝,从而为您提供不完整的结果。

将其更改为:

   <button type="button" id="get">get</button>
    <script type="text/javascript">
        var ele = document.getElementById('get');
        ele.addEventListener('click', testPromise, false);
        var getData = function (url) {
            var promise1 = new Promise(function (resolve, reject) {
                var xhr = new XMLHttpRequest();
                xhr.open('GET', url);
                xhr.setRequestHeader('Accept', 'application/json');
                xhr.send();
                xhr.onreadystatechange = handler;
                xhr.responseType = 'json';
                function handler () {
                    if (this.readyState === 4) {
                        console.log('status: ' + this.status);
                        if (this.status === 200) {
                            console.log(this.response);
                            resolve(this.response);
                        } else {
                            reject(new Error(this.statusText));
                        }
                    }
                }
            });
            return promise1;
        }

        function testPromise() {
            getData('http://1.frontendlife.sinaapp.com/test/promise.json').then(function (json) {
                console.log(json);
            }, function (error) {
                console.log(error);
            });
        }
    </script>