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')
        }
      }
    }
  • 相关阅读:
    CDN 机制
    canvas绘制旋转图形
    前端资源网站
    css中的em用法
    响应式网页设计【转载】
    闭包与非闭包
    跨域文档之间的访问
    ajax跨域之---服务器端代理实现
    jsonp跨域实现
    freemarker 命名空间
  • 原文地址:https://www.cnblogs.com/wuqilang/p/15386140.html
Copyright © 2011-2022 走看看