Combination of async function + await + setTimeout

前端 未结 12 1530
予麋鹿
予麋鹿 2020-11-22 04:34

I am trying to use the new async features and I hope solving my problem will help others in the future. This is my code which is working:

  async function as         


        
12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 05:25

    var testAwait = function () {
        var promise = new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('Inside test await');
            }, 1000);
        });
        return promise;
    }
    
    var asyncFunction = async function() {
        await testAwait().then((data) => {
            console.log(data);
        })
        return 'hello asyncFunction';
    }
    
    asyncFunction().then((data) => {
        console.log(data);
    });
    
    //Inside test await
    //hello asyncFunction
    

提交回复
热议问题