在外部回调中解决/拒绝承诺

Resolve/Reject Promis within external callback

本文关键字:拒绝 承诺 解决 外部 回调      更新时间:2023-09-26

我在玩ajax&iterated承诺为ajax调用创建一个抽象版本。下面的代码运行良好。

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);
        }
    };
}

然而,我想将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);
    }
}

不幸的是,我得到了解决和拒绝未知的错误。我知道问题只是chk1中的承诺是未知的。然而,我不确定如何解决它。

我希望能够这样称呼它:

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

亲切问候

您可以将resolvereject传递给checker函数,以便它可以根据需要调用它们。

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);
    }
}

献给那些仍然关心的人。jfriend00的答案是金子。我的最终解决方案是这样的(我不得不从使用这个改为另一个参数):

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);
    }
}