【发布时间】:2017-01-14 22:22:21
【问题描述】:
我已经开始阅读@angular 源代码,部分是为了学习,部分是因为某些文档似乎缺少一些东西。在表单指令中,我遇到过这样的代码块:
const resolvedPromise = Promise.resolve(null);
...
export class NgForm extends ControlContainer implements Form {
...
addControl(dir: NgModel): void {
resolvedPromise.then(() => {
...
});
}
}
这个具体的例子来自ng_form,这个模式在这个文件中重复出现。使用 this 的每个函数都没有返回值,我认为这是相关的。
使用这个resolvedPromise 的目的是什么?我的理解是,由于在调用此函数之前已经完成了resolvedPromise,因此提供给then 函数的回调将立即执行。在这种情况下,为什么还要使用 Promise?
【问题讨论】: