Skip to content

Instantly share code, notes, and snippets.

@Lazhari
Created December 31, 2016 17:19
Show Gist options
  • Save Lazhari/180d12be2a2e7fe38eb111d34f7bdba3 to your computer and use it in GitHub Desktop.
Save Lazhari/180d12be2a2e7fe38eb111d34f7bdba3 to your computer and use it in GitHub Desktop.
JavaScript generators using co and request-promise-native
const request = require('request-promise-native');
const co = require('co');
const getWebPage = co.wrap(function * (url) {
let body;
try {
body = yield request({
uri: url,
method: 'GET'
})
} catch (err) {
console.error('getGooglePage() error:', err);
throw err;
}
return body;
});
getWebPage('https://google.com')
.then(page => console.log(page));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment