LoginSignup
0
0

More than 1 year has passed since last update.

【メモ】非同期でhttpリクエストを実行する。

Posted at

こんな感じでhttpリクエストができる。(メモ程度)

members.component.ts
  ngOnInit(): void {
    const myAsync = async (url) => {
      const response = await fetch(url); //await で fetch() が完了するまで待つ
      const data = await response.json(); //await で response.json() が完了するまで待つ
      console.log('1');
      return data;
    }
    myAsync('http://localhost:8080/api/messages').then(value => this.members = value).catch((err) => this.disp('データの取得に失敗しました。'));
    console.log('2');
  }
members.component.ts
ngOnInit(): void {


    const myAsync = async (url) => {
      let d:any;
      const response = await fetch(url,{method: "POST",body: JSON.stringify(d)}); //await で fetch() が完了するまで待つ
      const data = await response.json(); //await で response.json() が完了するまで待つ
      console.log('1');
      return data;
    }
    myAsync('http://localhost:8080/api/messages').then(value => this.members = value).catch((err) => this.disp('データの取得に失敗しました。'));
    console.log('2');

  }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0