Ответ 1
async/await может работать в angularjs 1.5
если планировщик $q
изменен с помощью Bluebird
promises.
Добавив следующее к app.ts
export class Module {
app: ng.IModule;
constructor(name: string, modules: Array<string>) {
this.app = module(name, modules);
}
}
function trackDigests(app) {
app.run(["$rootScope",$rootScope => {
Promise.setScheduler(cb => {
$rootScope.$evalAsync(cb);
});
}]);
}
export var app: ng.IModule = new Module("app", []).app;
trackDigests(app);
Регулярный планировщик $q
заменяется.
Код вроде этого работает из коробки!
async $onInit() {
this.start();
try {
var key = await this.powerService.getKey(this._apiKey.partyAccountNumber);
var sites = await this.powerService.loadSites(this._apiKey.partyAccountNumber);
await this.showSummary(sites);
this.stop(true);
} catch (error) {
this.$log.error(error);
this.stop(false);
}
}
Вы можете вернуть Promise
или ng.IPromse
из своих методов обслуживания, поскольку они взаимозаменяемы.
export interface IPowerService {
setKey(key: pa.PowerApi): ng.IPromise<dm.ClientSite[]>;
getKey(partyAccountNumber: string): Promise<pa.PowerApi>;
}
Используя вышеприведенный trackDigests
в тестовой жгуте, модульные тесты также будут работать с async / await
В моем случае я добавил следующее в свой webpack.test.js
, чтобы включить async/await
, чтобы работать аналогично с тестами карма.
plugins: [
new webpack.ProvidePlugin({
Promise: 'bluebird'
})
],