async await 異步、等待、按順序執行


<template>
  <div>
  </div>
</template>
<script>
export default {
  name: 'AsyncAwait',
  data() {
    return {
    }
  },
  created(){
    this.getProvinces()
  },
  methods:{
    //await 只能用在async標注的函數中
    province(){
      return new Promise((resolve)=>{
      setTimeout(() => {
          resolve('省')
          console.log('省')
       }, 4000);
      })
      
    },
    city(){
      return new Promise((resolve)=>{
        setTimeout(() => {
          resolve('市')
          console.log('市')
       }, 2000);
      })
      
    },
    country(){
      return new Promise((resolve)=>{
        setTimeout(() => {
          resolve('縣')
          console.log('縣')
       }, 3000);
      })
      
    },
    async getProvinces(){
      await this.province()
      await this.city()
      await this.country()
    },
  }
}
</script>
<style scoped>

</style>

打印顯示的順序為:省 市 縣

如果在函數里
            this.$nextTick(async _ => {
    await this.province()
    await this.city()
    await this.country()
            })


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM