【问题标题】:.then handler being invoked out of order.then 处理程序被乱序调用
【发布时间】:2016-09-01 04:00:14
【问题描述】:

我的最终目标是将一大块数据库分配给基于类的对象。为此,我在.then() 函数的帮助下利用打字稿中的承诺来链接它们。但是我遇到了砖墙。

return(this.httpService.post(someurl, somepayload, someheaders)
  .toPromise()
  .then(response => response.json())
  .then(MyCustomClass.function(MyObject)));

但是,当此代码在获得 response.json() 之前执行 .then(MyCustomClass.function(MyObject)) 时,这会导致我的程序出现问题。

我的问题是,为什么它们会按此顺序发生,有什么办法可以强制它们按顺序执行?

【问题讨论】:

    标签: http angular promise


    【解决方案1】:

    您正在调用MyCustomClass.function(MyObject),并将返回值传递给then()。您真正想要的是传递一个函数,该函数在调用时将执行MyCustomClass.function(MyObject)

    .then(() => {
      MyCustomClass.function(MyObject);
    }));
    

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 2023-02-21
      相关资源
      最近更新 更多