Skip to content

Instantly share code, notes, and snippets.

@deadkff01
Created October 23, 2018 04:50
Show Gist options
  • Save deadkff01/91314ebba61a0b142c00ad27804fb42e to your computer and use it in GitHub Desktop.
Save deadkff01/91314ebba61a0b142c00ad27804fb42e to your computer and use it in GitHub Desktop.
JavaScript Generators
let https = require('https')
const httpGetAsync = (url, callback) => {
return https.get(url,
(response) => {
var body = ''
response.on('data', (d) => {
body += d
})
response.on('end', () => {
let parsed = JSON.parse(body)
callback(parsed)
})
}
)
}
// httpGetAsync('https://www.reddit.com/r/pics/.json', (picJson) => {
// console.log(picJson.data.children[0].data.url)
// httpGetAsync(picJson.data.children[0].data.url+'.json', (data) => {
// console.log(data)
// })
// })
const request = (url) => {
httpGetAsync(url, (response) => {
generator.next(response)
})
}
const main = function* () {
let pictureJson = yield request('https://www.reddit.com/r/pics/.json')
let firstPictureData = yield request(pictureJson.data.children[0].data.url + '.json')
console.log(firstPictureData)
}
let generator = main()
generator.next()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment