Code Monkey home page Code Monkey logo

expanded-generators's Introduction

Expanded Generators

The async/await proposal creates a new function type for sequencing Promises.

async function getStockPrice(name){
  let symbol = await getSymbol(name);
  let price = await getPrice(symbol);
  return price;
}
@push function* getStockPrices(name) {
  let symbol = await getSymbol(name),
    retryCount = 0;

  while(retryCount < 3){
    try {
      for*(let price on new Websocket('/' + symbol + '/prices')) {
        yield price;
      }
    }
    catch(e) {
      retryCount++;
      console.error(e);
    }
  }
}
// all driver functions must be generators to support clean up on side-exit
function* push(genF) {
  return new Observable(function(generator) {
    var gen = genF(),
      subscription;
      
      
    function step(nextF) {
      var next;
      try {
        next = nextF();
      } catch(e) {
        // finished with failure, reject the promise
        generator.throw(e); 
        return;
      }
      if (next.await) {
        // could be Promise from await or Observable from for*
        observable = Observable.from(next.value);
        subscription = observable[Symbol.observer]({
          // next is body of for*(...on...), yields 
          throw(e) {
            generator.throw(e);
          },
          return(v) {
            // executed if there's a break statement
          });
      }
      else if(next.done) {
        // finished with success, resolve the promise
        generator.return(next.value);
        return;
      } 
      else if (next.value && next.value.then) {
        // not finished, chain off the yielded promise and `step` again
        Promise.cast(next.value).then(function(v) {
          step(function() { return gen.next(v); });      
        }, function(e) {
          step(function() { return gen.throw(e); });
        });
      }
      else {
        // ES6 tail recursion prevents stack growth
        step(function() { return gen.next(next.value)});
      }
    }
    step(function() { return gen.next(undefined); });
  });
}  
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.