Last active
March 22, 2017 11:24
-
-
Save ppraksa/502f36fe366b6683ca8f9613e2084dae to your computer and use it in GitHub Desktop.
Combaine promise with generator
This file contains 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
//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