1 / 7

Generators, Real- ish Example

Generators, Real- ish Example . Presented By: Angello Pozo At: Downtown LA Node Meetup. Outline. Some* Details What Are Generators Thunks Examples Question /resources. Some* Details. Creating a Generator Iterating generator Using Generators Yield * Returns optional For.

sean-mejia
Download Presentation

Generators, Real- ish Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Generators,Real-ish Example Presented By: AngelloPozo At: Downtown LA Node Meetup

  2. Outline • Some* Details • What Are Generators • Thunks • Examples • Question/resources

  3. Some* Details • Creating a Generator • Iterating generator • Using Generators • Yield * • Returns optional • For • Gen = Function *() { }; • Gen.next() • node 0.11: --harmony flag • Allow generator method to control iteration • Be careful with return • for (varitGenof generator()) { console.log( itGen); }

  4. What Are Generators • An Iterable Function created at Yield • Think how arrays are really objects var first = function *(){ yield "First Place”; return 1; } varmyGen = function *() { var one = yeild*first(); //returns 1 var two = yeild *second(); //returns 2 var three = yeild*third(); //returns 3 } variterable = myGen(); var out1 = it.next() //returns { value: 'First Place', done: false } var out2 = it.next(); //returns { value: ’Second Place', done: false } var out3 = it.next(); //returns { value: 'Third Place', done: false } var out4 = it.next(); //returns { value: undefined, done: true } myGen = { 0 : first(), 1 : second(), 2 : third() }

  5. Thunks A thunkis a method that wraps around the original method, while controlling the callback function orig(data, cb){ //do operations return cb(null, data); } function thunk_orig(data){ returns function(cb){ orig(data, cb); } }

  6. Examples • Examples can be found at: • https://github.com/sogoiii/generator_uber_examples • We are going to use all 3 uber endpoints • Price • Product • Time • We are going to make one giant object containing each endpoint.

  7. Questions? • Resources: • http://davidwalsh.name/es6-generators • https://github.com/petkaantonov/bluebird/blob/master/API.md#generators • https://github.com/visionmedia/co#thunks-vs-promises • http://strongloop.com/strongblog/node-js-callback-hell-promises-generators/ • http://tobyho.com/2013/06/16/what-are-generators/

More Related