我是 angularjs 的新手,只有 1.5 周的经验。
我的服务正在返回 IPromise。我在 Controller 中使用它作为:
private GetHeader(): void {
this._SolutionExplorerHeaderData = this.globalShellService.SolutionExplorerHeaderData;
this.globalShellService.retrieveHeaderData().then(
(response: Models.SolutionExplorerHeader) => {
this._SolutionExplorerHeaderData = response;
});
}
我这样调用这个方法:
private InitComponents(): void {
let statusBar = new LayoutModels.StatusBar(Designer.DesignerTitleKey);
let designerShell = this.GetDesignerShell();
let globalCommadBarHandler = this.globalCommandBarService;
this.GetHeader();
this.headerModel = new LayoutManager.Models.HeaderModel(this._SolutionExplorerHeaderData.SolutionType,
this._SolutionExplorerHeaderData.Publisher,
this._SolutionExplorerHeaderData.InstalledOn,
this._SolutionExplorerHeaderData.ModifiedOn,
this._SolutionExplorerHeaderData.LastSaved,
this._SolutionExplorerHeaderData.LastPublished);
this.layoutModel = new LayoutManager.Models.LayoutModel(Designer.DesignerName,
designerShell, statusBar, globalCommadBarHandler, this.headerModel);
}
现在的问题是 GetHeader() 中的 .then() 将在调用 header 模型映射语句之前稍晚返回数据。
我应该如何 await(C#) @this.GetHeader() 语句以便在该语句处暂停执行。
最佳答案
确保您的 GetHeader 函数返回一个 promise ,因此返回then
调用的结果:
return this.globalShellService.retrieveHeaderData().then( //... etc
//^^^^
然后在代码的第二部分,继续使用then
:
this.GetHeader().then(_ => {
// ^^^^
this.headerModel = new LayoutManager.Models.HeaderModel(// ...etc
// ...
});
或者——如果您支持async
/await
——您可以将InitComponents
方法定义为async:
private async InitComponents(): void { // ...etc
// ^^^^^
并在调用 GetHeader
时使用 await
:
await this.GetHeader();
//^^^
this.headerModel = new LayoutManager.Models.HeaderModel(// ...etc
// ...
关于javascript - angularjs 1.5 中的等待功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45231867/