跟踪循环中触发的请求-响应
Keep track of requests-responses fired in a loop
考虑这个人为的例子
reqs = for url in urls
$.ajax
url: url
successs: (resp) =>
# here, url is always the last url,
# apparently because closure doesn't copy the captured locals
console.log "response for url: #{url}"
判断成功案例中哪个url属于哪个请求的最佳方式是什么?
稍后有来电
$.when(reqs...).then (resps...) =>
console.log(resps)
不过,现在知道了,因为 resps
的顺序可能与 reqs
的顺序不同,对吗?
感谢 James 的评论,解决方案是
reqs = for url in urls
success_fn = (url) =>
(resp) =>
console.log "response for url: #{url}"
console.log resp
$.ajax
url: url
successs: success_fn(url)
考虑这个人为的例子
reqs = for url in urls
$.ajax
url: url
successs: (resp) =>
# here, url is always the last url,
# apparently because closure doesn't copy the captured locals
console.log "response for url: #{url}"
判断成功案例中哪个url属于哪个请求的最佳方式是什么?
稍后有来电
$.when(reqs...).then (resps...) =>
console.log(resps)
不过,现在知道了,因为 resps
的顺序可能与 reqs
的顺序不同,对吗?
感谢 James 的评论,解决方案是
reqs = for url in urls
success_fn = (url) =>
(resp) =>
console.log "response for url: #{url}"
console.log resp
$.ajax
url: url
successs: success_fn(url)