Using yield in generator doesn't guarantee to wait until promise is full-filled.
To make your example works, you have to write the code to invoke next method of object that implements iterable protocol.
function runGenerator(asyncGen) {
const gen = asyncGen();
let returnValue;
(function iterate(value){
returnValue = gen.next(value);
if(!returnValue.done) {
returnValue.value.then(iterate);
}
})();
}
const object = {
getPastEvents: () => Promise.resolve([1,2,3])
};
runGenerator(function*() {
const values = yield object.getPastEvents();
console.log(values);
});
Note that this is just simple implementation, you have to check more conditions if you want to use actual projects.
Rather than just implementing your own for this, I would recommend to use co module.
Note that Async & Await is very similar to this approach, they both requires promisified API as well.
However to use Async & Await, make sure your JavaScript Engine supports, else you have to transpile to make them work in older engines.
But using generator will works most of modern JavaScript engine, it's quite bit old specification(ES6).
Also generally transpiling Async & Await makes lots of code which could be the problem if you want to save the content size as small as possible.
Key difference of ES6 Generator and ES7 Async & Await is that ES6 Generator can't use "Arrow function" which really critical some situations(you have to save the "this" reference somewhere before get into generator function's context), however ES7 Async Function can do that.
Note that ES7 Async & Await is just syntactic sugar of promise API, but ES6 generator doesn't.