Problems and Solutions of for loop nesting in asynchronous programming

Source: Internet
Author: User

Scenario:
Get () and set () are two asynchronous functions. In a for loop, I first get data and then set data.
The following code displays the following situations:

 
 
  1. for(var i=1;i<=1000;++i){
  2. var res = get("args"+i,function(err,data){
  3. set("args"+i,data,function(err,result){});
  4. })
  5. }
The above code will show this situation: when the set function is executed, I has changed to 1001. Why?
My personal understanding: Asynchronization does not wait, because the efficiency of the loop is higher than that of Asynchronization, so when it is set execution, I has changed to 1001;
How can this problem be solved?
I was so stupid at the time that I took my own services and wrote down it here to despise myself in the future:

 
 
  1. for(var i=1;i<=1000;++i){
  2. var res = get("args"+i,function(err,data){
  3. for(var i=1;i<=1000;++i){
  4. set("args"+i,data,function(err,result){});
  5. }
  6. });
  7. }
This is the case, but the set function has been executed for 1000*1000 times, and you have come up with this method.
After correction, I want to define a global variable and then set the function without using I.

 
 
  1. var j=1;
  2. for(var i=1;i<=1000;++i){
  3. var res = get("args"+i,function(err,data){
  4. set("args"+(j++),data,function(err,result){});
  5. });
  6. }
This is a solution, but it is not perfect. First, it is always bad to define a global variable. In addition, some people will inevitably see this code saying that I and j are not the same? Unless you comment out the previous sentence, who will remove this global j and I will be desperate.
So I thought of this method again:

 
 
  1. for(var i=1;i<=1000;++i){
  2. (function(idx){
  3. var res=get("args"+idx,function(err,data)
  4. set("args"+idx,data,function(err,result){});
  5. });
  6. })(i);
  7. }
To solve the problem of not defining a global variable externally, the anonymous function does not use the "Global" variable at all, which is much better.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.