Skip to content

Instantly share code, notes, and snippets.

@ppraksa
Last active March 22, 2017 11:24
Show Gist options
  • Save ppraksa/502f36fe366b6683ca8f9613e2084dae to your computer and use it in GitHub Desktop.
Save ppraksa/502f36fe366b6683ca8f9613e2084dae to your computer and use it in GitHub Desktop.
Combaine promise with generator
//Combaine promise with generator
function *getUrl(url) {
try {
var response = yield getReq(url).then(function(fb) {
console.log('feedback!!!');
console.log(fb);
return fb;
},
function(e) {
console.log(e);
return e;
});
} catch(e) {
console.log(e);
}
return 'done';
}
function getReq(url) {
return new Promise(function(res,rej) {
xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if (xhr.status === 200) {
res(xhr.responseText);
}
else if (xhr.status !== 200) {
rej('error');
}
};
xhr.send(null);
});
}
var it = getUrl('www.google.com');
//it.next();
//it.next().value;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment