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

前端 未结 2 2011
感动是毒
感动是毒 2021-02-01 17:21

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 w

2条回答
  •  天命终不由人
    2021-02-01 17:43

    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:

    • co
    • Task.js
    • bluebird's Promise.coroutine
    • q's spawn

    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.

提交回复
热议问题