javascript 在成功的调用中进行 ajax 调用可以被认为是不好的做法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18623202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making an ajax call inside a successful one can be considered bad practice?
提问by Alexandre Wiechers Vaz
Let's take the following piece of code:
让我们看下面的一段代码:
$.ajax({
type: 'POST',
dataType: dataType,
url: 'someUrl',
success: function(result){
$.ajax({
type: 'POST',
dataType: dataType,
url: 'anotherUrl',
data: queryToSearch,
success: function(anotherResult){
(do something that uses the first one result)
},
error: MyObj.defaultAjaxError
});
},
error: MyObj.defaultAjaxError
});
Can this be considered a bad practice? Does it have any hit on performance? If yes, is there a better way to do something like this?
这可以被视为一种不好的做法吗?它对性能有任何影响吗?如果是,有没有更好的方法来做这样的事情?
回答by user2246674
Use Promises. Hopefully, Promises/A(as implemented in jQuery 1.8+Deferred Objects), then:
使用承诺。希望Promises/A(在 jQuery 1.8+Deferred Objects 中实现),然后:
$.ajax({..}) // Promise 1
.fail(function () {
// Oops! This will fire if (and only if) Promise 1 failed.
})
.then(function () {
// This will only fire if the first request had no error - was "done"
// We then return a NEW promise for the 2nd request. In a proper
// Promises/A, 'then' returns a (new) promise. (jQuery < 1.8 is broken.)
return $.ajax({..}) // Promise 2
})
// Note that these are for the 2nd promise which has been returned from
// the 'then' above OR from a 2nd promise created automatically by the default
// failHandler.
.fail(function () {
// Oops! This will fire if EITHER the promises (AJAX calls) fails.
// This happens because we are either binding to our Promise 2
// or to the auto-rejected promise returned from the default failHandler.
})
.done(function () {
// 2nd promise done - means both are done!
})
Using when
is not appropriate, because that would be "in parallel". (Actually, when
couldbe used with a "stub" promise that is wired to be accepted when the 2nd call completes - however this doesn't benefit from then
chaining and it's not possible to meaningfully use the promise from the 2nd call directly for serial execution.)
使用when
是不合适的,因为那将是“并行的”。(实际上,when
可以与“存根”承诺一起使用,该承诺在第二次调用完成时被连接起来接受 - 但是这不会从then
链接中受益,并且不可能有意义地使用来自第二次调用的承诺直接进行串行执行。 )
One interesting thing to note is that fail
and done
are just shorthands for/restricted forms of then
. These methods can (and should) be used for clarity of intent/code.
需要注意的一件有趣的事情是fail
和done
只是then
. 这些方法可以(并且应该)用于意图/代码的清晰度。
回答by smurf brainy
If you need the callbacks to run sequentially, you need to do it this way. If they need to be done in parallel ( order is not guaranteed ), than you should not do it this way. It is not good or bad practice issue. It is a matter of what you need to accomplish.
如果您需要回调按顺序运行,则需要这样做。如果它们需要并行完成(不保证顺序),那么您不应该这样做。这不是好的或坏的实践问题。这是你需要完成什么的问题。
回答by Brian Maupin
There is nothing explicitly wrong with doing it this way, but you may look into using jQuery Deferred Objects.
这样做并没有什么明显的错误,但是您可以考虑使用jQuery Deferred Objects。