zoukankan      html  css  js  c++  java
  • 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()
                })
  • 相关阅读:
    Java API学习
    Java接口类学习笔记
    TCP/IP协议族
    Web服务器
    Redis笔记
    Python知识总汇
    GIL锁
    day8
    day7
    day6
  • 原文地址:https://www.cnblogs.com/hellofangfang/p/13578431.html
Copyright © 2011-2022 走看看