Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在承诺中传递论点_Javascript_Jquery_Promise - Fatal编程技术网

Javascript 如何在承诺中传递论点

Javascript 如何在承诺中传递论点,javascript,jquery,promise,Javascript,Jquery,Promise,我用的是这样的承诺: var restClient = { serveRequest: function(rUrl, type, body, rHeaders, rAsync, callback) { var promise = jQuery.ajax({ url: rUrl, type: type, data: body, headers: rHeaders,

我用的是这样的承诺:

var restClient = {
    serveRequest: function(rUrl, type, body, rHeaders, rAsync, callback) {  
        var promise = jQuery.ajax({
           url: rUrl,
           type: type,
           data: body,
           headers: rHeaders,
           async: rAsync,
           contentType: "text/plain",
           dataType: "json"
        });

        promise.then(onSuccess, onError);   
    },
    onSuccess: function(data) {
        callback(data);
    },
    onError: function(msg) {
        console.log(msg.responseText);
    }
}
如何在promise.then onSuccess中传递参数(回调)?我想稍后在onSuccess方法中使用它

我是这样用承诺的

首先,你不应该。承诺的目的是作为异步函数的结果返回,这样您就不再需要回调参数了。你最好就这么做

var restClient = {
    serveRequest: function(rUrl, type, body, rHeaders, rAsync) {  
        var promise = jQuery.ajax({
           url: rUrl,
           type: type,
           data: body,
           headers: rHeaders,
           async: rAsync,
           contentType: "text/plain",
           dataType: "json"
        });
        return promise;
    }
};
并让
restClient.serveRequest(…)
的调用者调用
。然后(…)

如何在promise.then onSuccess中传递参数(回调)

成功时不需要
这个
。直接使用

promise.then(callback, function(msg) {
    console.log(msg.responseText);
});
我想稍后在onSuccess方法中使用它

你不能。它试图使用
callback
,但这是
serveRequest
方法的本地参数-因此
onSuccess
最多只能是其中的本地函数,而不能是其自身的方法

我是这样用承诺的

首先,你不应该。承诺的目的是作为异步函数的结果返回,这样您就不再需要回调参数了。你最好就这么做

var restClient = {
    serveRequest: function(rUrl, type, body, rHeaders, rAsync) {  
        var promise = jQuery.ajax({
           url: rUrl,
           type: type,
           data: body,
           headers: rHeaders,
           async: rAsync,
           contentType: "text/plain",
           dataType: "json"
        });
        return promise;
    }
};
并让
restClient.serveRequest(…)
的调用者调用
。然后(…)

如何在promise.then onSuccess中传递参数(回调)

成功时不需要
这个
。直接使用

promise.then(callback, function(msg) {
    console.log(msg.responseText);
});
我想稍后在onSuccess方法中使用它


你不能。它试图使用
callback
,但这是
serveRequest
方法的一个局部参数-因此
onSuccess
最多只能是其中的一个局部函数,而不是它自己的一个方法。

我想看看在没有回调的情况下如何使用承诺。我不认为这是你真正的意思。@jfriend00:Fixed。我的意思是,除了
之外,没有任何函数或方法需要回调参数。我想看看在没有回调的情况下如何使用承诺。我不认为这是你真正的意思。@jfriend00:Fixed。我的意思是,除了
之外,没有任何函数或方法需要回调参数。