Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript yeoman生成器:从异步回调内部复制或模板不起作用_Javascript_Node.js_Asynchronous_Yeoman_Yeoman Generator - Fatal编程技术网

Javascript yeoman生成器:从异步回调内部复制或模板不起作用

Javascript yeoman生成器:从异步回调内部复制或模板不起作用,javascript,node.js,asynchronous,yeoman,yeoman-generator,Javascript,Node.js,Asynchronous,Yeoman,Yeoman Generator,在yeoman生成器中,我试图根据外部网络资源的状态进行条件复制。我的问题是,在异步回调(例如http请求的回调)中调用yeoman copy命令src.copy和template时,似乎也没有任何作用 示例代码,位于yeoman.generators.NamedBase.extend块内: main: function(){ //-> here this.copy('inlocation','outlocation') works as expected var that = thi

在yeoman生成器中,我试图根据外部网络资源的状态进行条件复制。我的问题是,在异步回调(例如http请求的回调)中调用yeoman copy命令src.copy和template时,似乎也没有任何作用

示例代码,位于yeoman.generators.NamedBase.extend块内:

main: function(){  
//-> here this.copy('inlocation','outlocation') works as expected
var that = this;

var appName = ...
var url = ...

var req = http.request(url, function(res){
//-> here that.copy('inlocation','outlocation') DOES NOT work
    res.on('data', function (data) {
        //console.log('Response received, onData event');
        //-> here that.copy('inlocation','outlocation') DOES NOT work
    });
    //-> here that.copy('inlocation','outlocation') DOES NOT work
});
req.on('error',function(error){
    //...
});
req.end();
//-> here this.copy('inlocation','outlocation') works as expected, once again
注意用“/->”注释标记的位置作为参考点-当它工作时,它会按预期工作。如果没有,控制台上就没有任何输出,因此.copy似乎作为一个函数存在,事实上我可以断言该类型的.copy=='function'!,没有错误消息,只是没有创建文件。通常的文件创建消息也丢失,这是正常工作命令的一个特征

使用call或apply将显式this引用传递给函数不会改变行为,也不会将其绑定到异步函数

对这种行为的解释是什么,以及如何以这种异步方式进行复制调用?

根据Eric MORAND的评论,我将把我找到的解决方案作为一个单独的答案发布,而不是对原始帖子进行编辑,希望它更容易找到:

我找到了一个解决方案,使用yeoman RunContext的异步函数。请参见异步代码开头的api文档中的以下行:

var done = this.async();

然后,在我想要运行copy之前,调用done使其按最初的预期运行。

您应该用找到的解决方案回答自己的问题。这将有助于其他用户确定您的问题已解决…并奖励您的声誉工作。