简体   繁体   English

如何按顺序执行javascript?

[英]How to execute javascript sequentially?

I expected the output: A, B, C. But it doesn't work. 我期望输出:A,B,C。但它不起作用。 Provided that the function handleClick(element) cannot be changed, how can I change the other functions to make sure that all code execute in sequence and output A, B, C as expected? 如果函数handleClick(element)无法更改,如何更改其他函数以确保所有代码按顺序执行并按预期输出A,B,C?

 async function handleClick(element) { setTimeout(function(){ console.log(`Click on Element_${element}`); } , Math.random(5)*1000); } async function clickLetter(letter) { await handleClick(letter); } async function clickGroup(group) { await handleClick(group); } const letters = ['A', 'B', 'C']; function clickLetters(letters, fn) { let index = 0; return new Promise(function(resolve, reject) { function next() { if (index < letters.length) { fn(letters[index++]).then(next, reject); } else { resolve(); } } next(); }); } clickLetters(letters, clickLetter); 

As mentioned, the async library is/was a way to do it. 如前所述, async库是一种方法。

If you want to keep going with your current solution, the problem was in your handleClick() where the implicit promise was returning immediately, before the timeout. 如果你想继续使用当前的解决方案,问题出在你的handleClick() ,其中隐含的promise在超时之前立即返回。 Then, depending on the random timeout for each pass, it would result in an out or ordr execution. 然后,根据每次传递的随机超时,它将导致out或ordr执行。 The fix is just resolve the promise then it times out. 修复只是解决了承诺然后超时。

 function handleClick(element) { return new Promise(resolve => { setTimeout(function(){ console.log(`Click on Element_${element}`); resolve(); } , Math.random(5)*1000); }); } async function clickLetter(letter) { await handleClick(letter); } function clickLetters(letters, fn) { let index = 0; return new Promise(function(resolve, reject) { function next() { if (index < letters.length) { fn(letters[index++]).then(next, reject); } else { resolve(); } } next(); }); } const letters = ['A', 'B', 'C']; clickLetters(letters, clickLetter); 

Read about async and await here. 阅读有关异步的信息并在此等待。

Here is the code. 这是代码。 Explanation is at the end. 说明就在最后。

 async function handleClick(element) { return new Promise((resolve, reject) => { setTimeout(function(){ console.log(`Click on Element_${element}`); resolve(); }, Math.random(5)*1000); }); } async function clickLetter(letter) { await handleClick(letter); } async function clickGroup(group) { await handleClick(group); } const letters = ['A', 'B', 'C']; async function clickLetters(letters, fn) { for(let i=0; i<letters.length; i++) { await clickLetter(letters[i]); } } clickLetters(letters, clickLetter); 

Explanation of what changes you will need - 解释您将需要进行哪些更改 -

  • STEP 1 - Convert your handleClick function to return a Promise . 第1步 - 转换handleClick函数以返回Promise

  • STEP 2 - Use await in your clickLetters function to print the values sequentially. 第2 clickLetters - 在clickLetters函数中使用await按顺序打印值。

The setTimeout function is asynchronous and the result is returned immediately, you need to wrap this inside a promise constructor and then resolve it. setTimeout函数是异步的,结果立即返回,您需要将其包装在promise构造函数中,然后解析它。

 function handleClick(element) { return new Promise((resolve, reject) => { setTimeout(() => { console.log(`Clicked on Element_${element}`); resolve(); }, Math.random(5) * 1000); }); } async function clickLetter(letter) { await handleClick(letter); } async function clickGroup(group) { await handleClick(group); } const letters = ['A', 'B', 'C']; async function clickLetters(letters, fn) { for(let i = 0; i < letters.length; i++) { await clickLetter(letters[i]); } } clickLetters(letters, clickLetter); 

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

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