12 March 2021

toPromise() Angular 2 (async await)

 

#1 Async Short - ToPromise

2021

const data = await this.chatManagementService.getBots().toPromise();
console.log(data);

#2 Async Function - ToPromise

2021
async ngOnInit() {
    await this.getBots();
}
/**
 * Async function
 * GET ALL BOT
 */
private async getBots(): Promise < void > {
    await this.chatManagementService.getBots()
    .toPromise()
    .then(value => {
        console.log(value)
    })
    .catch(error => console.log(error));
}

Related Posts:

  • Ng serve --live-reload trong Angular 2 Sử dụng ng serve --live-reload chạy lệnh ng serve --live-reload trên terminal Hoặc thêm lệnh ng serve --live-reload vào file package.json và chạy npm start trên terminal … Read More
  • ControlValueAccessor TwoWay Binding trong Angular 2+  ControlValueAccessor TwoWay Binding interface ControlValueAccessor { writeValue(obj: any): void; registerOnChange(fn: any): void; registerOnTouched(fn: any): void; setDisabledState?(isDisabled: boolean… Read More
  • HostListener event scroll Angular 2+ Event scroll Angular 2+ Angular 2+ import { Component, HostListener, Inject, NgModule, OnInit } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { D… Read More
  • HostListener event key Angular 2+ Event key Angular 2+ Angular 2+ @HostListener('window:keydown', ['$event']) keyboardInput(event: any) { event.preventDefault(); event.stopPropagation(); this.which = event.which; this.keyCode = … Read More
  • Cách sử dụng Directives trong Angular 2+1. Directive Attribute. Bước 1: Tạo project Angular 2 bấm vào đây Bước 2: Tạo directive có tên là highlight ng generate directive highlight src/app/app.component.html Angular 2 <h1>My First Attri… Read More

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang