[英]how to await a promise in a function before getting to next line
How to wait the getstatus(statustable).then(function(response)
to do before going to console.log("old");
where getstatus is an async function will return a new promise如何等待
getstatus(statustable).then(function(response)
to do before to console.log("old");
其中 getstatus 是一个异步函数将返回一个新的承诺
var count = "old";
getstatus(statustable).then(function(response){
console.log("changeing count")
count = "new";
console.log(count)
});
console.log("old");
Put console.log("old");
把
console.log("old");
inside .then
.里面
.then
。 Anything you want to execute for sure after the promise you will have to put it inside the .then
or another .then
chained after it.任何你想在 promise 之后确定执行的东西,你都必须把它放在
.then
或另一个.then
后面。
If you want to write it in a sequential way you will have to use async/await .如果您想以顺序方式编写它,则必须使用async/await 。
You can await on getStatus你可以在 getStatus 上等待
var count = "old";
await getstatus(statustable).then(function(response){
console.log("changeing count")
count = "new";
console.log(count)
});
console.log("old");
The whole function that this is a part of will need to be async.这是它的一部分的整个函数需要是异步的。 If you can't change the function to async then you'll need to do as @Tushar Shahi suggested and put the last console log inside another then like below
如果您无法将功能更改为异步,那么您需要按照@Tushar Shahi 的建议进行操作,并将最后一个控制台日志放在另一个中,然后如下所示
var count = "old";
getstatus(statustable).then(function(response){
console.log("changeing count")
count = "new";
console.log(count)
}).then (() => {
console.log("old");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.