gpt4 book ai didi

javascript - 链接的 `.then()` 能否以与 `.then()` 的顺序不同的顺序返回响应?

转载 作者:行者123 更新时间:2023-11-30 16:05:40 25 4
gpt4 key购买 nike

在下面的示例中,服务器是否可能响应 doSomethingOne s 请求在服务器响应 doSomethingTwo 之前返回的要求?

$scope.pace = $interval (
function() {

// code

}, 1000, 10

).then(postResult)
.then(doSomethingOne)
.then(doSomethingTwo);

问题是因为,doSomethingOne正在将数据发布到数据库,然后是 doSomethingTwo正在查询数据库并返回一些数据,包括 doSomethingOne 的数据应该已经发布了。

但是对doSomethingTwo的回应的请求不包括 doSomethingOne 发布的最新数据(直到 $scope.pace 从头开始​​再次运行)。

我对回调没有很深的理解(尽管阅读了很多关于它们的资料)所以任何建议都将不胜感激。

简要说明

doSomthingOne$http.post()doSomethingTwo$http.get() .这里没有使用 promise 。

最佳答案

更新:

来自您的编辑:

doSomthingOne does an $http.post() and doSomethingTwo does an $http.get(). No promises are used here.

好吧,$http.post returns a promise (有时在文档中称为“ future ”),但如果您不使用它,则没有什么可以阻止 doSomethingTwo 在 POST 完成之前被调用。事实上,它非常可能会在 POST 完成之前(很久之前)被调用。

您可以简单地通过返回 promise $http.post 返回来解决问题(或者返回您调用该 promise 所创建的 promise ,如果您正在使用的话)。例如:

function doSomethingOne() {
return $http.post(/*...args...*/);
}

function doSomethingOne() {
return $http.post(/*...args...*/).then(/*...*/);
}

详情如下。

原始答案(仍然相关):

这取决于 doSomethingOne 做了什么以及它返回了什么。如果 doSomethingOne 启动了一个异步进程,但没有返回该进程的 promise ,则可以在该进程完成之前调用 doSomethingTwo。如果 doSomethingOne 同步完成它的工作(不太可能按照你所说的)或返回其异步工作的 promise ,它将在调用 doSomethingTwo 之前完成,因为 doSomethingTwo 等待该 promise 得到解决。

这是一个示例,其中 doSomethingOne 不为其异步工作返回 promise ,因此 doSomethingTwo 很可能在 doSomethingOne 之前运行' s 异步工作完成:

// Simulate an asynchronous DB call
function dbCall(data) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Result for " + data);
}, Math.floor(Math.random() * 500));
})
}
function start() {
console.log("start called");
return new Promise(resolve => {
setTimeout(() => {
console.log("start resolving");
resolve();
}, 0);
})
}
function doSomethingOne() {
// THIS IS PROBABLY WRONG
console.log("doSomethingOne called");
dbCall("one data").then(function(result) {
console.log("doSometingOne's async is done");
});
}
function doSomethingTwo() {
console.log("doSomethingTwo called");
}
start().then(doSomethingOne).then(doSomethingTwo);

Live copy on Babel's REPL

这可能是错误的。相反,您希望 doSomethingOne 从其异步工作中返回一个 promise;它可以通过返回调用 dbCall(...).then 的结果来做到这一点:

// Simulate an asynchronous DB call
function dbCall(data) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Result for " + data);
}, Math.floor(Math.random() * 500));
})
}
function start() {
console.log("start called");
return new Promise(resolve => {
setTimeout(() => {
console.log("start resolving");
resolve();
}, 0);
})
}
function doSomethingOne() {
console.log("doSomethingOne called");
return dbCall("one data").then(function(result) {
//^^^^^^^------------------------------------------- change is here
console.log("doSometingOne's async is done");
});
}
function doSomethingTwo() {
console.log("doSomethingTwo called");
}
start().then(doSomethingOne).then(doSomethingTwo);

Live copy on Babel's REPL

关于javascript - 链接的 `.then()` 能否以与 `.then()` 的顺序不同的顺序返回响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37100074/

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