How to return an Ajax result using async/await?

后端 未结 2 1520
面向向阳花
面向向阳花 2020-11-28 11:40

Trying to get familiar with async/await, I\'ve tried the following code in Chrome:

async function f() { 
     return await $.get(\'/\');
};
va         


        
2条回答
  •  暖寄归人
    2020-11-28 12:23

    either

    function f() { 
      return $.get('/');
    };
    
    async test() {
      var x = await f()
      console.log(x)
    }
    
    test()
    

    or

    f().then(function(res) {
        console.log(res)
    }
    

    the async/await is just another way to write the same logic.

提交回复
热议问题