node.js 意外的严格模式保留字——yield?节点 v0.11,和声,es6
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28414973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unexpected strict mode reserved word -- yield? Node v0.11, harmony, es6
提问by Robert Taylor
Trying to use a new ES6 based node.js ODM for Mongo (Robe http://hiddentao.github.io/robe/)
尝试为 Mongo 使用新的基于 ES6 的 node.js ODM(Robe http://hiddentao.github.io/robe/)
Getting "unexpected strict mode reserved word" error. Am I dong something wrong here?
收到“意外的严格模式保留字”错误。我在这里有什么问题吗?
test0.js
测试0.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
回答by alexpods
If you want to use generatorsto 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 corealization you can find in:
这里co实现你可以找到:
and so on.
等等。
In strict modeyou cannot use yieldoutside of the generators. In non-strict modeoutside of the generators yieldwill be considered as variable identifier - so in your case it'll throw an error anyway.
在strict mode不能使用yield发电机之外。在non-strict mode生成器的外部 yield将被视为变量标识符 - 因此在您的情况下它无论如何都会抛出错误。
回答by Robert Taylor
Also noteworthy... new versions of co return/use promises rather than thunks. So this is what worked with newer versions of co.
同样值得注意的是......新版本的共同返回/使用承诺而不是重击。因此,这适用于较新版本的 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);
});

