Generator Functions using co

Generator functions are a concept lifted likely from Python where they are used heavily in applications like web-crawlers. There are no prerequisites to this article but if you had an idea about Python generators, its the exact same concept implemented in Javascript.

Definition

  1. Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.
  2. Generators are cooperative, which means that it chooses when it will allow an interruption so that it can cooperate with the rest of the program.

Syntax

Generators are declared using the special * keyword right after the word function

function* foo(){
 let x = yield "value for x'
 let y = yield "value for y'
 let z = yield "value for z'

 return (x+y+z)
}

Notice another special keyword yield which is specific to generator functions.

yield/*yield

  1. yield is where the magic happens inside a function. It is used to pause the function and also give out a value.
  2. yield can also take in a value when we restart the function.
  3. *yield is use to delegate to another outside generator function. This means that it will pause, delegate to another generator function, complete it, and then come back to the original generator function.

co

co is Generator based control flow goodness for nodejs and the browser, using promises, letting you write non-blocking code in a nice-ish way.

lets first do the prerequisites

npm install node-fetch
npm install co

now the sample code:

const fetch = require('node-fetch')
const co = require('co')

co(function *(){
    const url = 'https://jsonplaceholder.typicode.com/posts/1'
    const response = yield fetch(url)
    const post = yield response.json()
    const title = post.title
    console.log('Title:', title)
})

The stuff sent as parameter to co() is the generator function.

Tek Shinobi
Author: Tek Shinobi

Hiya Ninjas, I am the ninja who invented the fire, wheel, science, technology and everything intelligent this humanity has ever experienced since its evolution from monkeys and germs. Actually, above is a partial list. I also was involved with the Big Bang that created this universe. What was it like before the Big Bang is so secret that if I tell you, I will have to turn you into a hobbit and force you to be my gardener. Okay. Sayonanra, Namaste Tek Shinobi




No Comments


You can leave the first : )



Leave a Reply

Your email address will not be published. Required fields are marked *