zoukankan      html  css  js  c++  java
  • js数组中的每一项异步请求

    数组中的每一个选项,都进行一次异步请求,然后所有请求完成再操作返回值,Promise.all


        getInfo = (param) => {
            const { getData } = this.props // getData为接口请求
            return new Promise(resolve => {
                getData ({
                    params: {
                        param
                    }
                }).then(result => {
                    if (result.statusCode === 200){
                        resolve(result.data)
                    } else {
                        resolve({})
                    }
                }).catch(() => {
                    resolve({})
                })
            })
        }

         getArrayInfo= () => {
             const  inputList = [1,2,3,4]
             const list = []
             inputList.map(item=> {
                     const temp = this.getInfo(item)
                     list.push(temp)
             })
             Promise.all(list).then(res => {
       //res为返回的接口数据,数组形式
               console.log(res)
             })
         }
  • 相关阅读:
    android-为应用单元测试
    android手机拨号器实现
    android模拟器使用
    android开发环境搭建
    C语言之关键字
    linux shell脚本基础-----3
    linux shell脚本基础-----2
    linux shell脚本基础-----1
    Android学习计划
    MySql 绿色版配置
  • 原文地址:https://www.cnblogs.com/tutuj/p/14603398.html
Copyright © 2011-2022 走看看