Restangular:等待承诺被解决?

5

我刚接触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。但是我担心自己不太理解这个问题,并且/或者已经感到非常沮丧/疲倦了。

欢迎回答和任何建议,特别是好的阅读材料指引。谢谢


1
你无法在那里返回实际值,因为 Restangular 是异步的(getSlices 在调用 then 回调之前已经完成)。这就是为什么使用 Promise。所以正确的方式是返回 Promise 并进行如下操作:service.getSlices().then(function(resultOfModification) { }); - t.niese
从我目前所了解的情况来看,我已经想象到这可能是不可行的,尽管我希望有一些方法可以打破这种异步性。我该如何使getSlices()返回一个承诺,其中包含我的resultOfModification - omilke
1个回答

10
在那个位置无法返回实际值,因为“Restangular”是异步的(函数“getSlices”在传递给“then”的回调被调用之前已经完成)。这就是为什么要使用“Promise”的原因。
即使可能使“Restangular”同步,也不应该这样做,因为这会阻塞浏览器,直到请求数据,这将是一个糟糕的用户体验。
你应该尝试了解“Promise”,因为它们被设计成看起来像同步代码但行为却是异步的。
在你的代码中需要更改的是,在“Restangular.all”之前添加一个“return”:
  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();
      })
  };

这将返回由.then调用返回的Promise。该Promise将解析为resultOfModification,因为这是您从回调函数返回的值。

通过这种方式,您可以使用getSlices

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

  });

Promise可以链接起来使用:

  (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);
  });

这听起来很不错。我迫不及待地想在回到工作岗位后尝试一下。到目前为止,非常感谢! - omilke
运行完美!您关于链式 Promise 的解释非常有帮助:D - omilke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接