我正在尝试在C#中执行类似async / await的功能,但是在node js中,我找到了一个示例,但这给了我一个错误。
这是代码
function* gotNews(response){
console.log("in gotNews");
str='';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
str = JSON.parse(str);
console.log(str);
fetchCategories();
});
return str;
}
function fetchNews(sourceURL){
console.log("in fetch news");
sourceURL = url.parse(sourceURL);
console.log(sourceURL);
var options =
{
host: sourceURL.host,
port: 134,
path: sourceURL.path,
method: 'GET',
};
var req = http.request(options,yield gotNews);//start request and recive response in gotSources
req.end();
}
我正在使用* yield操作,但给我一个错误
ErrorC:\Users\Alaa\Desktop\Fluid_layout_with_jQuery_Masonry\1\app.js:198
var elnewselygat = yield gotNews();
^^^^^^^
SyntaxError: Unexpected identifier
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
你需要的发电机,并承诺使它一样容易async
/ await
/ Task
。
如@Paul所述,生成器是“未来功能”,因此您需要传递--harmony
或--harmony-generators
。此外,V8 3.19(仅在Node.js 0.11.2或更高版本中)支持生成器。
JavaScript社区当前正在考虑多种可能的方法。有几个人一个很好的概述,这里,还有其他的库出有作为。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句