zoukankan      html  css  js  c++  java
  • promise的正确写法(规避回调地狱的写法)

    import axios from 'axios'
    export default {
      mounted() {
        // this.getTodos().then((res) => {
        //   console.log('todos', res.data)
        //   this.getComments().then((res) => {
        //     console.log('comments', res.data)
        //     this.getAlbums().then((res) => {
        //       console.log('albums', res.data)
        //     })
        //   })
        // })
        // 当请求之间有依赖关系时,上面的写法会导致回调地狱,推荐下面的写法
        this.getTodos()
          .then((res) => {
            console.log('todos', res.data)
            return this.getComments()
          })
          .then((res) => {
            console.log('comments', res.data)
            return this.getAlbums()
          })
          .then((res) => {
            console.log('albums', res.data)
          })
      },
      methods: {
        getTodos() {
          return axios.get('https://jsonplaceholder.typicode.com/todos?_limit=5')
        },
        getComments() {
          return axios.get('https://jsonplaceholder.typicode.com/comments?_limit=5')
        },
        getAlbums() {
          return axios.get('https://jsonplaceholder.typicode.com/albums?_limit=5')
        }
      }
    }
  • 相关阅读:
    Spring5.0源码导入IDEA(一)
    适配器模式
    3.6常见查询示例
    3.5在批处理模式下使用mysql
    3.4获取有关数据库和表的信息
    3.3.4.9使用多个表
    3.3.4.8计数行
    3.3.4.7模式匹配
    3.3.4.6使用NULL值
    3.3.4.5日期计算
  • 原文地址:https://www.cnblogs.com/wuqilang/p/15386140.html
Copyright © 2011-2022 走看看