私は2つのコンポーネントを持っています。子コンポーネントにはイベントがあります(EventEmitter)。親コンポーネントには、長時間のメソッドを持つハンドラがあります。Observable callでイベント加入者の完了を待つ方法
私のメソッド(longTimeMethod)の実行が完了するのを待つ必要があります。方法は5秒近くまで続きます。
すべてのステップの実行に一貫して到達したい(1 - > 2 - > 3 - > 4 - > 5)。
しかし、今のシーケンスは1である - 私が使用して非同期待つアプローチを試みた> 3
@Component({
selector: 'child-selector',
template: '<>********</>'
})
export class ChildComponent{
@Output() childEvent = new EventEmitter();
someFunction() {
// Step 1: event emit
this.childEvent.emit();
// Step 5: Subscribers execution finished
// Some code which should to reached after all event subscribers
}
}
@Component({
selector: 'parent-selector',
template: '<child-selector (childEvent)="handleChildEvent()"></child-selector>'
})
export class ParentComponent{
// Step 2: handler start
handleChildEvent() {
// this.longTimeMethod return Observable<void>
this.myService.longTimeMethod()
.subscribe(() => {
// Step 3: Method executing finished
});
// Step 4: Another code
}
}
> 2 - > 4 - - > 5:シーケンスが変更されたが、それはまだありません
async handleChildEvent() {
// this.longTimeMethod return Observable<void>
await this.myService.longTimeMethod().toPromise()
.then(() => {
// Step 3: Method executing finished
});
// Step 4: Another code
}
を正しい:1 - > 2 - > 5 - > 3 - > 4 正しい動作を達成するには?
お返事ありがとうございます。これは私の場合に役立ちます。 – x666ep