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

Question on for...on loop synchronicity #13

Open
benlesh opened this issue Feb 22, 2015 · 3 comments
Open

Question on for...on loop synchronicity #13

benlesh opened this issue Feb 22, 2015 · 3 comments

Comments

@benlesh
Copy link

benlesh commented Feb 22, 2015

EDIT: talking about the block itself, not the inner loop code

Will for...on blocks (not the inner loop, specifically) be asynchronous, synchronous, or sometimes both?

I ask this because I view for...on to be analogous to RxJS's Observable.prototype.subscribe(). And the behavior there is (IMO) confusing for developers, as it's sometimes synchronous, sometimes not.

To be clear, I'm not asking about whether the emitted values are observed asynchronously, I'm asking about the act of subscription to the observable, specifically.

For example, in RxJS:

let source = Observable.fromArray([1,2,3]);
console.log('start');
source.forEach(x => console.log(x));
console.log('end');

/*
start
1
2
3
end
*/

but given a different observable source, it behaves differently:

// some source the returns each thing asynchronously
let source = Observable.create(o => {
  setTimeout(() => {
    o.onNext(1);
  });
  setTimeout(() => {
    o.onNext(2);
  });
  setTimeout(() => {
    o.onNext(3);
  });
});
console.log('start');
source.forEach(x => console.log(x));
console.log('end');

/*
start
end
1
2
3
*/

From the transpiled example in your README, I've surmized that the intent seems to be that subscription to an observable with for...on will be asynchronous, so that in all cases, the following should be true:

async function* nums() {
  yield 1;
  yield 2;
  yield 3;
}
let source = nums();
console.log('start');
for(var x on source) {
  console.log(x);
}
console.log('end');

/*
start
end
1
2
3
*/

If that is the case, should current Observable libraries strive to imitate this same behavior? Will async generators, in effect, be Observables? (i.e. carry the same interface as RxJS or Most.js like a forEach)? Should Observables themselves have some standardization similar to what A+ has done from Promises?

@calebboyd
Copy link

I think because of an async-generator's inherent relationship with promises that async behavior is guaranteed (via A+) already. Fixing the implementations in RxJS or others should probably happen, to re-align with where the standards are, and their direction (composing async function* => observable).

@benlesh
Copy link
Author

benlesh commented Feb 26, 2015

@calebboyd: I made that issue you referenced. :) and it's whole-heartedly related to my question here.

It seems to me that subscription should occur asynchronously by default, while event emission should occur synchronously, for performance reasons.

@calebboyd
Copy link

@Blesh Yep, I agree. Would that imply a for(...on) loop is an implicit subscription?

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