AyKarsi
AyKarsi

Reputation: 9675

Node Generators: Using yield at top-level

I'm trying to create a job, which reuses modules from my express app which strongly relies on node-harmony. (And which works nicely), but I don't know how to use generators at "top-level".

So I have this file job.js

var locator = require('./../locatorSetup');
yield locator.connect(); // returns a promise
console.log('connected');

Which I start by calling

node --harmony job.js

Unfortunately I get:

yield locator.connect();
      ^^^^^^^
SyntaxError: Unexpected identifier

What's the recommend way of doing this?

P.S. I'm using Bluebird as my promise library..

Upvotes: 1

Views: 518

Answers (1)

Dan
Dan

Reputation: 10538

You can't. yield can only be used within a generator function.

Consider using co to execute generator functions at a top level, like so:

co(function *() {
  yield locator.connect();
});

co returns a promise that you can then use to track the completion of the passed generator function.

Upvotes: 3

Related Questions