I am trying to understand how Generators can work with promises and would like to know how to return the resolved value of a promise in a Generator.
The following code contains an Unexpected identifier
error. I think this is because the yield
is in the .done()
callback but I am not sure. How can I get the result of the promise when I call next() on the generator?
Here is my code:
function *genPromise(url) {
$.getJSON(urlArray)
.done( res => {
yield res.data.children[1].data.url;
});
}
var url = 'https://www.reddit.com/r/reddits.json';
var x = genPromise(url)
console.log(x.next());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>