可以链接 '.then() 的返回响应,顺序与 '.then()' 的顺序不同
Can chained `.then()`s return responses in a different order than order of `.then()`s?
在下面的示例中,服务器对doSomethingOne
请求的响应是否有可能在服务器响应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()
。这里没有使用任何承诺。
更新:
从您的编辑:
doSomthingOne 做一个 $http.post((,doSomethingTwo 做一个 $http.get((。这里没有使用任何承诺。
好吧,$http.post
返回一个承诺(有时在文档中称为"未来"(,但是如果您不使用它,那么没有什么可以阻止在 POST 完成之前调用doSomethingTwo
。事实上,它很可能在 POST 完成之前(很久以前(被调用。
您可能只需返回承诺$http.post
返回即可解决问题(如果您正在使用承诺,则返回由调用该承诺创建的承诺(。 例如:
function doSomethingOne() {
return $http.post(/*...args...*/);
}
或
function doSomethingOne() {
return $http.post(/*...args...*/).then(/*...*/);
}
详情如下。
原始答案(仍然相关(:
这取决于doSomethingOne
做什么以及它返回什么。如果doSomethingOne
启动异步进程,但没有返回该进程的承诺,则可以在该进程完成之前调用doSomethingTwo
。如果doSomethingOne
同步完成其工作(鉴于您所说的不太可能(或为其异步工作返回承诺,它将在调用doSomethingTwo
之前完成,因为doSomethingTwo
等待该承诺得到解决。
下面是一个示例,其中 doSomethingOne
不会为其异步工作返回承诺,因此doSomethingTwo
可能会在 doSomethingOne
的异步工作完成之前运行:
// 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);
Babel's REPL 上的实时副本
这可能是错误的。相反,您希望doSomethingOne
从其异步工作中返回承诺;它可以通过返回其调用的结果来做到这一点 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);
Babel's REPL 上的实时副本
- ajax请求的顺序总是不同的
- 按照选项卡索引的顺序循环一个jQuery选择
- 匹配一个单词,其中候选人可以跨越顺序组(跨度)
- 按顺序添加和删除类
- 按我自己的类克隆另一个元素的内容和顺序
- Javascript-根据赋值顺序,按键合并对象数组
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- querySelector/getElementByClassName嵌套项的顺序
- Javascript:If-then语句在函数中不起作用
- angularjs无法读取未定义的属性then
- 以不同的顺序输出数据
- 重新排列HTML元素的顺序并更改内容
- 在Javascript中列出顺序子集元素
- 是否“;对于的“;循环迭代遵循JavaScript中的数组顺序
- Mongodb$in以与数组中相同的顺序获取结果
- 如何使用phonegap从安卓设备中提取联系人的字母顺序
- 在表单OnChange中交换数字顺序
- 如何按对象反转Angular ngRepeat的顺序's键,这些键是数字键
- 可以链接 '.then() 的返回响应,顺序与 '.then()' 的顺序不同
- 为什么 then() 链式方法不按顺序运行