node.js – 在yeoman-generator中的this.async()

我正在学习如何写一个自耕农发电机.我对以下代码有疑问.它通过添加var done = this.async()来说并且稍后在回调中调用方法,我们可以使函数askFor()成为异步函数.有人可以解释一下原因吗?
askFor: function() {
    var done = this.async();

    // Have Yeoman greet the user.
    this.log(yosay('Welcome to the marvelous Myblog generator!'));

    var prompts = [{
        name: 'blogName',message: 'What do you want to call your blog?',default: 'myblog'
    }];

    this.prompt(prompts,function(props) {
        this.blogName = props.blogName;

        done();
    }.bind(this));
}

这是this.async的代码

this.async = function() {
    return function() {};
}

解决方法

只是通过纯粹的巧合寻找其他东西而陷入这个问题.

实际上,在运行阶段,每个方法都会覆盖this.async,以延迟执行直到完成或同步运行.

您可以在此处阅读相关代码行:
https://github.com/yeoman/generator/blob/master/lib/base.js#L372-L393

所以基本上,在幕后Yeoman总是叫回调.当你调用this.async()时,我们保留一个引用变量并返回回调.如果你不调用它,我们会在函数结束后手动调用回调.

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...