Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serial loop support #1

Closed
guileen opened this issue May 18, 2011 · 4 comments
Closed

serial loop support #1

guileen opened this issue May 18, 2011 · 4 comments
Assignees

Comments

@guileen
Copy link
Owner

guileen commented May 18, 2011

var a;
for(var i=0; i < 100; i++){
  a = foo(a, _);
}
console.log(a);

compile to serial loop

target code 1

var a, i=0;
function next(){
  foo(a, function(err, _a){
   if(err) return cb(err);
   a = _a;
   i++;
   if(i<100){
      next();
    } else {
      console.log(a);
    }
  }
}
next();

target code 2

var a;
function next(i, after){
  foo(a, function(err, _a){
   if(err) return cb(err);
   a = _a;
   if(i<100) next(++i);
   else after()
  }
}
function after(){
  console.log(a);
}
next(0, after);

which one is better?

@ghost ghost assigned guileen May 18, 2011
@guileen
Copy link
Owner Author

guileen commented May 19, 2011

for(var i = 0;i<100;i++){
  a+=i;
  b = foo(a, _);
}

loop_block readed a, and updated a, but not update a in async mode, so, next a do not need to wait previous foo callback.

for(...){
  a = foo(_);
  b = bar(a, _);
}

a is readed and updated, but foo can be parallel, a=_a;b=bar(a, _); can be execute one after another

for(...){
  a = foo(b, _);
  b = bar(a);
}

foo depends b depends a depends foo, so foo depends foo, this should be serial...

what is the key logic...

for(...){
  a = foo(b, _);
  b = bar(_);
}

foo depends b depends bar of pervious loop

@guileen
Copy link
Owner Author

guileen commented May 19, 2011

also take care for(var i..) res.push(foo(i, _));, the single statement without {}

@fengmk2
Copy link

fengmk2 commented May 20, 2011

next() 是递归?

@guileen
Copy link
Owner Author

guileen commented May 20, 2011

是递归

guileen added a commit that referenced this issue May 21, 2011
guileen added a commit that referenced this issue May 21, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants