unexpected strict mode reserved word — yield? Node v0.11, harmony, es6

浪尽此生 提交于 2019-12-02 22:13:45

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.

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);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!