在下面的函数中,我试图返回第二次异步调用的promise对象,但是typescript抱怨该函数必须返回一个值。我不知道我怎么能做到这一点?我如何归还第二个承诺?
layoutSubviews()
答案 0 :(得分:0)
var requests = {
firstRequest: function() {
return $.ajax('https://jsonplaceholder.typicode.com/posts/1');
},
secondRequest: function() {
return $.ajax('https://jsonplaceholder.typicode.com/posts/2');
}
};
var processResults = {
callService: function() {
return requests.firstRequest().then((data, textStatus, promise) => {
return requests.secondRequest().then((data2, textStatus2, promise2) => {
return "This is the content of second call: id=>" + data2.id;
});
});
}
};
processResults.callService().then((data, textStatus, promise) => {
console.log(data);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
注意:
return promise2
代替return "This is the content of second call: id=>"+ data2.id;
以下是您与return promise2;
var requests = {
firstRequest: function() {
return $.ajax('https://jsonplaceholder.typicode.com/posts/1');
},
secondRequest: function() {
return $.ajax('https://jsonplaceholder.typicode.com/posts/2');
}
};
var processResults = {
callService: function() {
return requests.firstRequest().then((data, textStatus, promise) => {
return requests.secondRequest().then((data2, textStatus2, promise2) => {
return promise2;
});
});
}
};
processResults.callService().then((data, textStatus, promise) => {
console.log(data);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;