2014-09-04 113 views
4

我新的Javascript和AngularJS而这一次让我抓我的头:/Restangular:等待承诺的决心?

前提

  • 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 


    }; 

问题

Bascially我想在getSlices()等到应许是为了回报解析我的resultOfModification只有当它实际计算。

其他情形
我也像返回从getSlices()承诺那么这将提供resultOfModification。不过,我担心我不会很好地理解这一点,并且/或者同时感到很沮丧/厌倦。

答案和任何建议,欢迎,尤其是指向良好的阅读材料。谢谢

+1

您无法返回它在那个地方作为实际值,因为'Restangular'是异步的('getSlices'在调用'then'回调之前被留下)。这就是为什么使用'Promise'的原因。所以_correct_方法将返回Promise,并执行:'service.getSlices()。then(function(resultOfModification){});'? – 2014-09-04 19:31:54

+0

我已经成像了,这可能是不可行的,虽然我希望可能有某种方式打破这种异步。我将如何使'getSlices()'返回一个包含我的'resultOfModification'的承诺? – omilke 2014-09-04 19:36:20

回答

9

你不能在那个地方作为实际值返回它,因为Restangular是异步的(在调用then的回调被调用之前,功能getSlices被留下)。这就是为什么使用Promise

即使有可能使Restangular同步,您也不应这样做,因为这会阻止浏览器,直到数据被请求,这将是一个糟糕的用户体验。

您应该尝试进入Promise,因为它们设计为看起来像同步代码但行为异步。

你需要在你的代码改变世界的事情是增加一个returnRestangular.all

service.getSlices = function() { 
     return 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(); 
     }) 
    }; 

这将返回Promise是由.then调用返回。这个承诺将解决到resultOfModification,因为这是你回报的价格。

这样,你可以使用getSlices这样:

service.getSlices().then(function(modifiedData) { 

    }); 

承诺可以链接起来:

(new Promise(function(resolve, reject){ 
    setTimeout(function() { 
     resolve("some"); 
    },200); 
    })) 
    .then(function(data) { 
    return data+' data'; 
    }) 
    .then(function(data) { 
    //here a Promise is return which will resovle later (cause of the timeout) 
    return new Promise(function(resolve, reject) { 
     setTimeout(function() { 
     resolve(data+' !!!!!!'); 
     },200); 
    }); 
    }) 
    .then(function(data) { 
    //this will have 'some data !!!!!!' 
    console.log(data); 
    }); 

这将是,如果你会写这样的说法相同:

var promiseA = new Promise(function(resolve, reject){ 
    setTimeout(function() { 
     resolve("some"); 
    },200); 
    }); 

    var promiseB = promiseA.then(function(data) { 
    return data+' data'; 
    }) 


    var promiseC = promiseB.then(function(data) { 
    //here a Promise is return which will resovle later (cause of the timeout) 
    return new Promise(function(resolve, reject) { 
     setTimeout(function() { 
     resolve(data+' !!!!!!'); 
     },200); 
    }); 
    }); 

    var promiseD = promiseC.then(function(data) { 
    //this will have 'some data !!!!!!' 
    console.log(data); 
    }); 
+0

这听起来不错。一旦我回来工作,我都渴望尝试。非常感谢! – omilke 2014-09-05 04:56:35

+0

作品perferctly!你对链接承诺的解释最有帮助:D – omilke 2014-09-05 18:32:15