27

Trying to use a new ES6 based node.js ODM for Mongo (Robe http://hiddentao.github.io/robe/)

Getting "unexpected strict mode reserved word" error. Am I dong something wrong here?

test0.js

"use strict";
// Random ES6 (works)
{ let a = 'I am declared inside an anonymous block'; }

var Robe = require('robe');

// :(
var db1 = yield Robe.connect('127.0.0.1');

Run it:

C:\TestWS>node --version
v0.11.10

C:\TestWS>node --harmony test0.js

C:\TestWS\test0.js:12
var db1 = yield Robe.connect('127.0.0.1');
          ^^^^^
SyntaxError: Unexpected strict mode reserved word
    at exports.runInThisContext (vm.js:69:16)
    at Module._compile (module.js:432:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:349:32)
    at Function.Module._load (module.js:305:12)
    at Function.Module.runMain (module.js:490:10)
    at startup (node.js:123:16)
    at node.js:1031:3
Robert Taylor
  • 309
  • 1
  • 3
  • 7

2 Answers2

22

If you want to use generators to do asynchronous operation in synchronous fashion you must do it like:

co(function*() {
    "use strict";

    { let a = 'I am declared inside an anonymous block'; }

    var Robe = require('robe');

    var db1 = yield Robe.connect('127.0.0.1');
})();

where co realization you can find in:

and so on.

In strict mode you cannot use yield outside of the generators. In non-strict mode outside of the generators yield will be considered as variable identifier - so in your case it'll throw an error anyway.

alexpods
  • 42,853
  • 9
  • 92
  • 91
  • Awesome. Thank yo, Alex, Kind of a brain-dead moment for me. Makes sense. 1.) Need to actually yield from within something yieldable (i.e. a generator 2.) Use said generator in something that can execute it for you transparently (co, Task.js etc) – Robert Taylor Feb 10 '15 at 01:11
1

Also noteworthy... new versions of co return/use promises rather than thunks. So this is what worked with newer versions of co.

var co = require('co');

co(function*() {
    "use strict";

    { let a = 'I am declared inside an anonymous block'; }

    var Robe = require('robe');

    var db1 = yield Robe.connect('127.0.0.1/swot');
    console.log(db1)

    return db1;

}).then(function (value) {
    console.log(value);
}, function (err) {
    console.error(err.stack);
});
Robert Taylor
  • 309
  • 1
  • 3
  • 7