【发布时间】:2015-10-06 23:07:15
【问题描述】:
我正在学习如何编写一个 yeoman-generator。我对下面的代码有疑问。它说通过添加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() {};
}
【问题讨论】:
-
你已经了解同步和异步javascript的区别了吗?
-
是的,我明白其中的区别。但我不明白它是如何通过使用
this.async()使代码在这里异步的
标签: node.js asynchronous yeoman-generator