Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Support Generators function #860

@tusharmath

Description

@tusharmath

As far as I understand async functions can also be implemented via observables. Wouldn't it be great if someone could write code like this —

var write = rx.fromCallback(fs.write);

rx.wrapGenerator(function * () {
  var bytesWritten = yield write('some-data'); 
  console.log('Bytes Written:', bytesWritten);
})

wrapGenerator

function wrapGenerator(func) {
  var gen = func();

  function attach(observable) {
    if (observable) {
      observable.subscribeOnNext(function(out) {
        setTimeout(function() {
          attach(gen.next(out.value));
        });
      });
    }
  }
  attach(gen.next().value);
}

Why Do this?
A lot of times we write async code using co library, it turns out to be much easier to read and is using promises internally. Now one can convert all observerables into promises using the asPromise method and then use the co lib OR have a way to use yield directly on the observable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions