首页 > 其他 > 详细

async await 异步、等待、按顺序执行

时间:2020-08-28 22:54:38      阅读:87      评论:0      收藏:0      [点我收藏+]
<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>

打印显示的顺序为:省 市 县

async await 异步、等待、按顺序执行

原文:https://www.cnblogs.com/hellofangfang/p/13578431.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!