Created
October 23, 2018 04:50
-
-
Save deadkff01/91314ebba61a0b142c00ad27804fb42e to your computer and use it in GitHub Desktop.
JavaScript Generators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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