简体   繁体   English

在外部回调中解析/拒绝Promis

[英]Resolve/Reject Promis within external callback

I am playing with ajax & iterated promises to create an abstracted version for a ajax call. 我正在玩ajax和迭代的promises来为ajax调用创建一个抽象的版本。 The below code is working fine. 以下代码工作正常。

function ajax(url){
    var xhr = {
        rq : function (method, url){
            var prom = new Promise(function (resolve, reject){
                var client = new XMLHttpRequest();
                //only accept 
                client.open(method,url,true);
                client.send();
                client.onload = function (){
                    if(this.status == 200){
                        if(this.response == "1"){
                            reject(this.response);
                        }
                        else{
                            resolve(this.response);
                        }
                    }
                    else{
                        reject(this.statusText);
                    }
                };
            });
            return prom;
        }
    };
    return {
        'get' : function(){
            return xhr.rq('GET', url);
        }
    };
}

However I want to abstract the function on ajax.onload to be passed as callback and I was thinking about something like this: 但是我想抽象ajax.onload上的函数作为回调传递,我正在考虑这样的事情:

function ajax(url, checker){
    var xhr = {
        rq : function (method, url){
            var prom = new Promise(function (resolve, reject){
                var client = new XMLHttpRequest();
                //only accept 
                client.open(method,url,true);
                client.send();
                client.onload = checker;
                };
            });
            return prom;
        }
    };
    return {
        'get' : function(){
            return xhr.rq('GET', url);
        }
    };
}

function chk1(){
    if(this.status == 200){
        if(this.response == "1"){
            reject(this.response);
        }
        else{
            resolve(this.response);
        }
    }
    else{
        reject(this.statusText);
    }
}

Unfortunately I get the error that resolve and reject are unknown. 不幸的是,我得到了解决和拒绝未知的错误。 I kind of know that the problem is just that within chk1 the promise is not known. 我有点知道问题只是在chk1内,这个承诺是未知的。 I am unsure however, how to resolve it. 但我不确定如何解决它。

I want to be able to call it something like this: 我希望能够这样称呼它:

Promise.all([ajax(u,chk1).get(),ajax(v,chk1).get()])
.then(callback.success)
.catch(callback.error);

Kind Regards 亲切的问候

You can pass resolve and reject to the checker function so it can call them as needed. 您可以将resolvereject传递给检查器功能,以便它可以根据需要调用它们。

function ajax(url, checker) {
    var xhr = {
        rq: function(method, url) {
            var prom = new Promise(function(resolve, reject) {
                var client = new XMLHttpRequest();
                //only accept 
                client.open(method, url, true);
                client.send();
                client.onload = function() {
                    checker.call(this, resolve, reject);
                };

            });
            return prom;
        }
    };
    return {
        'get': function() {
            return xhr.rq('GET', url);
        }
    };
}

function chk1(resolve, reject) {
    if (this.status == 200) {
        if (this.response == "1") {
            reject(this.response);
        } else {
            resolve(this.response);
        }
    } else {
        reject(this.statusText);
    }
}

To anyone who still cares. 对任何仍在乎的人。 jfriend00's answer is gold. jfriend00的答案是黄金。 My final solution looks like this (I had to change from using this to another paramter): 我的最终解决方案看起来像这样(我不得不从使用此更改为另一个参数):

function ajax(url, checker){
    var xhr = {
        rq : function (method, url){
            var prom = new Promise(function (resolve, reject){
                var client = new XMLHttpRequest();
                //only accept 
                client.open(method,url,true);
                client.send();
                client.onload = checker(resolve, reject, client);
                });
            return prom;
        }
    };
    return {
        'get' : function(){
            return xhr.rq('GET', url);
        }
    };
}

function chk1(resolve, reject, xhr){
    if(xhr.status == 200){
        if(xhr.response == "1"){
            reject(xhr.response);
        }
        else{
            resolve(xhr.response);
        }
    }
    else{
        reject(xhr.statusText);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM