为什么这项服务 return 是承诺而不是兑现的承诺?
Why does this service return a promise rather than a fulfilled promise?
我理解 Angular promises,当 Angular 向 $http
发出请求时,.then(function(res){ return res })
应该等到承诺被解决。因此,如果我将请求附加到一个变量,变量的值不应该是已解决的承诺吗?
服务调用GithubAPI
is.service('githubApiService', function($http) {
this.repo = function(user, repo) {
var value = $http.get('https://api.github.com/repos/' + user + '/' + repo).then(function(json) {
return {
stargazers_count: json.data.stargazers_count,
watchers_count: json.data.watchers_count,
forks_count: json.data.forks_count,
watchers: json.data.watchers,
subscribers_count: json.data.subscribers_count
};
});
return value;
};
})
调用服务的指令
is.directive('chart', function(githubApiService) {
return {
restrict: 'E',
template: '<svg margin="[20, 30, 30, 40]", width="750" height="450"></svg>',
link: function(scope, element, attrs) {
var ais = githubApiService.repo('srph', 'angular-infinite-scroll');
var ai = githubApiService.repo('thenikso','angular-inview');
var ns = githubApiService.repo('jankuca', 'ng-scroller');
console.log(ai); // returns a promise!
}
}
})
这是预期的行为。它returns所有场景下的promise,你需要使用.then()
获取你想要的数据:
githubApiService.repo('thenikso','angular-inview').then(function(data){
var ai = data;
});
您无法像您期望的那样获得承诺的价值。
如果您查看 $q documentation,您将看到您链接的 then
方法 returns 也是一个承诺:
This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback (unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining)
因此您必须通过成功处理程序获取返回值:
githubApiService.repo(...).then(function (data) {
// data is the object you are expecting
});
它returns promise 因为你不知道底层的异步调用什么时候会完成。
在一些微不足道的情况下,这可能会立即发生,但直到您通过 then
方法明确 'resolve' 履行或拒绝的承诺,您才拥有实际的承诺。
考虑这两种方法,其中 doSomething
returns 一个承诺:
myService.doSomething().then(function(data){
return data;
});
myService.doSomething().then(function(data){
//This is some non-trivial calculation that will take a few seconds to do
var myData = transformData(data);
return myData;
});
Promise 允许您以相同的方式处理这两个调用,您必须使用 then
方法调用来解决它,而实际上不必担心数据是否已准备好程序。
我理解 Angular promises,当 Angular 向 $http
发出请求时,.then(function(res){ return res })
应该等到承诺被解决。因此,如果我将请求附加到一个变量,变量的值不应该是已解决的承诺吗?
服务调用GithubAPI
is.service('githubApiService', function($http) {
this.repo = function(user, repo) {
var value = $http.get('https://api.github.com/repos/' + user + '/' + repo).then(function(json) {
return {
stargazers_count: json.data.stargazers_count,
watchers_count: json.data.watchers_count,
forks_count: json.data.forks_count,
watchers: json.data.watchers,
subscribers_count: json.data.subscribers_count
};
});
return value;
};
})
调用服务的指令
is.directive('chart', function(githubApiService) {
return {
restrict: 'E',
template: '<svg margin="[20, 30, 30, 40]", width="750" height="450"></svg>',
link: function(scope, element, attrs) {
var ais = githubApiService.repo('srph', 'angular-infinite-scroll');
var ai = githubApiService.repo('thenikso','angular-inview');
var ns = githubApiService.repo('jankuca', 'ng-scroller');
console.log(ai); // returns a promise!
}
}
})
这是预期的行为。它returns所有场景下的promise,你需要使用.then()
获取你想要的数据:
githubApiService.repo('thenikso','angular-inview').then(function(data){
var ai = data;
});
您无法像您期望的那样获得承诺的价值。
如果您查看 $q documentation,您将看到您链接的 then
方法 returns 也是一个承诺:
This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback (unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining)
因此您必须通过成功处理程序获取返回值:
githubApiService.repo(...).then(function (data) {
// data is the object you are expecting
});
它returns promise 因为你不知道底层的异步调用什么时候会完成。
在一些微不足道的情况下,这可能会立即发生,但直到您通过 then
方法明确 'resolve' 履行或拒绝的承诺,您才拥有实际的承诺。
考虑这两种方法,其中 doSomething
returns 一个承诺:
myService.doSomething().then(function(data){
return data;
});
myService.doSomething().then(function(data){
//This is some non-trivial calculation that will take a few seconds to do
var myData = transformData(data);
return myData;
});
Promise 允许您以相同的方式处理这两个调用,您必须使用 then
方法调用来解决它,而实际上不必担心数据是否已准备好程序。