Async & await


async

  • は、より単純で読み取り可能になることを約束しています.
  • 関数返却承諾
  • async function helloAsync() {
      return 'hello Async';
    }
    
    helloAsync().then((res) => {
    	console.log(res) // 'hello Async'
    })
    上のコードがpromiseを使ってsettimeoutを使うとしたら?
    function delay(ms) {
      return new Promis((res) => {
      	setTimeout(res, ms)
      })
    }
    
    async function helloAsync() {
      return delay(3000).then(() => {
        return 'hello Async';
      })
    }
    
    helloAsync().then((res) => {
    	console.log(res) // 3초 후에 'hello Async'
    })

    await


    上のコードawaitを使うとしたら?
    function delay(ms) {
      return new Promis((res) => {
      	setTimeout(res, ms)
      })
    }
    
    async function helloAsync() {
      await delay(3000);  //간단해짐
      return 'hello Async';
    }
    
    helloAsync().then((res) => {
    	console.log(res) // 3초 후에 'hello Async'
    })
  • awaitを非同期関数呼び出しに使用する前に、同期関数と同じ動作をします.
    (非同期関数遅延終了後に「hello async」を返す)
  • async関数でのみ使用可能な
  • ->helloAsync()呼び出しもasyncに変更した場合?

    function delay(ms) {
      return new Promis((res) => {
      	setTimeout(res, ms)
      })
    }
    
    async function helloAsync() {
      await delay(3000);  //간단해짐
      return 'hello Async';
    }
    
    async function main() {
    	const res = await helloAsync();
     	console.log(res);
    }
    
    main();
  • main()コール
  • helloAsync()呼び出し(完了するまで次のコードを実行しない)
  • コール
  • delay(買収:3000)
  • Timeoutを設定して
  • で運転を遅延し、3秒後に
  • を完了する.
  • hello async()を実行する次の行コードreturn「hello async」
  • コンソールは
  • main()の次の行コードです.実行ログ(res)
  • jsonデータのインポート方法

    async function getData() {
      	let rawResponse = await fetch('주소');
      	let jsonResponse = await rawResponse.json();
    	console.log(jsonResponse);
    }
    getData();
    
    asyncでawaitを使用して順序付け공부하며 정리&기록하는 ._. 씅로그