我刚接触JavaScript和AngularJS,这个让我感到困惑:/
前提条件
- 一个REST服务从后端提供我的数据
- AngularJS 1.2.21和Restangular 1.4.0
- 一个AngularJS控制器,它将请求服务提供的数据的增强版本
我拥有什么
这是疑问的方法:
service.getSlices = function() {
Restangular.all('entries').getList().then(function(entries) {
//some rather complex modification of the backend data go here
//...
return resultOfModification; //this is what should be returned for getSlices();
})
//I want the resultOfModification to be returned here
};
问题
基本上,我想在getSlices()
中等待承诺被解决,以便只有在计算出resultOfModification
时才返回它。
附加场景
我也可以想象从getSlices()
返回一个承诺,然后提供resultOfModification
。但是我担心自己不太理解这个问题,并且/或者已经感到非常沮丧/疲倦了。
欢迎回答和任何建议,特别是好的阅读材料指引。谢谢
Restangular
是异步的(getSlices
在调用then
回调之前已经完成)。这就是为什么使用Promise
。所以正确的方式是返回 Promise 并进行如下操作:service.getSlices().then(function(resultOfModification) { });
? - t.niesegetSlices()
返回一个承诺,其中包含我的resultOfModification
? - omilke