How to use generators to avoid passing callback functions in JavaScript