Case:
() This is an asynchronous method, which handles some actual business. At this time, the printout is likely to be 300, 300, 300 (because the asynchronous for loop has not yet waited for the asynchronous operation to return to the Promise object, and the i value has changed to 300)
function getMoney(){ var money=[100,200,300] for(let i=0; i<; i++){ ().then(()=>{ (money[i]) }) } }
1. Async/await processing ideas
async function getMoney(){ var money=[100,200,300] for( let i=0; i<; i++){ await ().then(()=>{ (money[i]) }) } } // async tells the getMoney method that there is an asynchronous operation// awaitPut specific asynchronous operations(method)Front,It means waiting for the asynchronous returnPromiseWill continue the subsequent operation
2. Recursive processing ideas
function getMoney(i) { var money=[100,200,300] ().then(() => { if ( i < ) { (money[i]); i++; getMoney(i); } }); } getMoney(0);//Start the call//Recursion to realize self-loop(The specific cycle isthenin,Ensure the front()The asynchronous operation is completed).then()Yes returnedPromiseThe object isresolveIt only took place later(Can learn about itPromiseObject)
Summarize
This is the article about Javascript processing asynchronous operations of loops. For more related contents of Javascript loops, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!