我有兩個有關AngularJS承諾的問題:如何創建我自己的承諾?
- 我該如何創建自己的承諾?
請參見下面的代碼:
function controller($http) {
var dataCache;
function getData(){
if(dataCache != null){
// should return my own promise here
// to pass the value of 'dataCache' to 'then' immediately
} else {
return $http.get('...some url ...');
}
}
}
- 如何返回最後一個承諾?
代碼:
function controller($http) {
var urlArr = ['url1', 'url2', 'url3'];
function getDataOneByOne() {
// should call $http.get() for the url in the 'urlArr' one after another in a chain
// and return the last promise
}
}
https://docs.angularjs.org/api/ng/service/$q – Ian 2014-09-06 01:37:16
請在將來寫每個帖子一個問題。嘗試以對社區最有幫助的方式撰寫你的帖子(未來的觀衆)。 – m59 2014-09-06 01:56:13