co

How do a I terminate a co.js loop after execution?


Whenever I run a co generator loop, nothing happens after execution the process just hangs. How do I terminate after completion?

co(function *() {
    // code;
})

Solution

  • Co() returns a promise. Simply return from the loop and handle it with .then(). Here we return the string "done", send that to console.log, and terminate the process. Any errors will be printed to the console.

    co(function *() {
        return "done";
    }).then(
      res => { console.log(res); process.exit() }
    ).catch(err => console.error(err) );