简体   繁体   English

如何使异步变为同步

[英]How to make async become sync

I'm still trying to figure out how the async/await works.我仍在尝试弄清楚 async/await 是如何工作的。

 function func1() { console.log('a'); func2(); console.log('b'); } async function func2() { console.log('1'); let value = await new Promise(resolve => setTimeout(function() { resolve('async') } , 2000)); console.log(value) console.log('2'); } func1();

How can I achieve output as below?如何实现如下输出?

a
1
async
2
b

I try to make func2 as async and await the setTimeout, but then they whole func2 become async function.我尝试将 func2 设为异步并等待 setTimeout,但随后它们整个 func2 都变成了异步函数。 So it will print out this instead:所以它会打印出来:

a
1
b    
async
2

You can't make async into sync你不能让异步变成同步

But you can get the output you need但是你可以得到你需要的输出

make func1 async and use await使 func1 异步并使用 await

 async function func1() { console.log('a'); await func2(); console.log('b'); } async function func2() { console.log('1'); let value = await new Promise(resolve => setTimeout(function() { resolve('async') } , 2000)); console.log(value) console.log('2'); } func1();

Synchronous means things are happening chronologically one after the other.同步意味着事情一个接一个地按时间顺序发生。 Asynchronous means things are happening chronologically independent of one another.异步意味着事情发生的时间顺序彼此独立。

When you execute a task synchronously, you wait for it to finish before moving on to another task(one after the other).当您同步执行任务时,您会等待它完成,然后再继续执行另一个任务(一个接一个)。

When you execute a task asynchronously, you can move on to another task before the prior task finishes.当您异步执行任务时,您可以在前一个任务完成之前继续执行另一个任务。 But it can't be exactly guranteed.但不能完全保证。

So, in your code you've call an async function inside a sync funcion.因此,在您的代码中,您在同步函数中调用了异步函数。 So, accoding to above explanation, you can not gurantee that after calling async function func2() rest of the func1() will execute.所以,根据上面的解释,你不能保证在调用异步函数 func2() 之后 func1() 的其余部分会执行。 Because func2() is an async funcation which you can't say how much time will it take l to end up its execution process.因为 func2() 是一个异步函数,你不能说它需要多少时间来结束它的执行过程。 So, You can't make async become sync.所以,你不能让异步变成同步。

function func1() {
   console.log('a');
   func2(); // call a asyncronized method inside a syncronized method
   console.log('b');
}

async function func2() {
   console.log('1');
   let value = await new Promise(resolve => setTimeout(function() { resolve('async') } , 2000));
   console.log(value)
   console.log('2');
  }
func1();

But you can call that async function func2() inside func1() using await keyword so at the same time func1() will need to convert into an async function in order to use await keyword.但是您可以使用 await 关键字在 func1() 中调用异步函数 func2() ,因此同时 func1() 需要转换为异步函数才能使用 await 关键字。 So, in this way your desired output can be generated.因此,通过这种方式可以生成您想要的输出。

// change func1() as async method
async function func1() {
   console.log('a');
   await func2(); // use await keyword
   console.log('b');
}

async function func2() {
   console.log('1');
   let value = await new Promise(resolve => setTimeout(function() {resolve('async') } , 2000));
   console.log(value)
  console.log('2');
}

func1();

I hope that this will help you to understand the problem.我希望这会帮助你理解这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM