gpt4 book ai didi

http - .then 处理程序被乱序调用

转载 作者:可可西里 更新时间:2023-11-01 17:00:31 27 4
gpt4 key购买 nike

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

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

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

我的问题是,为什么它们会按那个顺序出现,有什么办法可以强制它们按顺序执行吗?

最佳答案

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

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

关于http - .then 处理程序被乱序调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39257675/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com