我有以下延迟对象:
var base = xhr.get({
url: config.baseUrl + base_query,
handleAs: "json",
load: function(result) {
widget.set('value', result);
},
error: function(result) {
}
});
当这个 GET 请求完成后,我需要使用第一个 base
结果的 URL 执行第二个请求:
var d1 = base.then(
function(result) {
xhr.get({
url: config.baseUrl + result.id,
handleAs: "json",
load: function(result) {
widget.set('visibility', result);
},
error: function(result) {
}
})
},
function(result) {
}
);
效果很好。但是我如何根据 base
结果发出两个或多个请求(例如 d1
)?是否可以将任何 d1
、d2
、...、dn
组合到一个延迟对象中并使用 then
连接它code> 到 base
对象?
最佳答案
是的,完全正确。您可以在 base
上无限次调用 then
:
var d1 = base.then(fn1),
d2 = base.then(fn2),
…
请注意,虽然它目前可能工作正常,但您的 d1
并不代表任何结果 - 由于您没有从回调中返回任何内容,因此链已损坏。您实际上应该返回第二个请求的 promise :
var base = xhr.get({
url: config.baseUrl + base_query,
handleAs: "json"
});
base.then(widget.set.bind(widget, 'value'));
// or: dojo.hitch(widget, widget.set, 'value') if you like that better
var d1 = base.then(function(result) {
return xhr.get({
// ^^^^^^
url: config.baseUrl + result.id,
handleAs: "json"
});
});
d1.then(widget.set.bind(widget, 'visibility'));
关于javascript - Dojo xhr 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17505677/