账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Promise的顺序优点迷,axios的顺序是逆序来的?
    • 2019-08-21 00:00
    • 10
    35
    0
    1. 需要实现的效果,reloadTrack彻底执行完,把isReload设置为false

    现在的效果是逆序的,莫名其妙,后axios的反而先执行,console.log的顺序如下

    1已在图上的路线为:
    2返回的轨迹数据为:
    bbefore3新加载的路线为:0
    bbefore3新加载的路线为:1
    bbefore3新加载的路线为:2
    before3新加载的路线为:2   18
    3新加载的路线为:2   18
    4更改状态为:false
    before3新加载的路线为:1   18,17
    before3新加载的路线为:0   18,17,15
    reloadTrack () {
          if (!this.isReload) {
            this.isReload = true
            console.log('1已在图上的路线为:' + this.tracksId)
            axios.post('/apb/api/track/init-map-track', {bounds: this.mapBounds, tracks: this.tracksId}).then((res) => {
              if (res.status === 200) {
                const data = res.data
                console.log('2返回的轨迹数据为:')
                console.log(data)
                this.mapTrack = data['tracks']
                this.addGpx()
              }
            })
          }
        },
        // 显示屏幕范围内的轨迹
        addGpx () {
          for (let m = 0; m < this.mapTrack.length; m++) {
            console.log('bbefore3新加载的路线为:' + m)
            axios.get('/apb' + this.mapTrack[m].gpx_path).then((res) => {
              if (res.status === 200) {
                this.geoJson = {} // 数据清空
                this.geoJson = toGeoJSON.gpx((new DOMParser()).parseFromString(res.data, 'text/xml'))
                let id = this.mapTrack[m].id
                // 记录已经加载的轨迹ID
                this.tracksId.push(id)
                console.log('before3新加载的路线为:' + m + '   ' + this.tracksId)
                let rateDifficulty = this.mapTrack[m].rate_difficulty
                let level = 1
                if (rateDifficulty.indexOf('A') >= 0) {
                  level = 1
                } else if (rateDifficulty.indexOf('B') >= 0) {
                  level = 2
                } else if (rateDifficulty.indexOf('C') >= 0) {
                  level = 3
                } else if (rateDifficulty.indexOf('D') >= 0) {
                  level = 4
                } else if (rateDifficulty.indexOf('E') >= 0) {
                  level = 5
                }
                // 增加轨迹图层数据源
                this.addTrackLayer(id, level)
                let data = {
                  'id': id,
                  'name': this.mapTrack[m].name,
                  'rate': this.mapTrack[m].rate_quality,
                  'distance': this.mapTrack[m].distance,
                  'level': level
                }
                this.addTrackPopup(data)
                // 主路线:增加起点终点
                if (id === parseInt(this.id)) {
                  this.geoJsonMain = this.geoJson
                  let lngLatData = this.geoJson.features[0].geometry.coordinates
                  let urls = ['/api/img/map/pinStart1.png', '/api/img/map/pinEnd1.png']
                  let trackLen = this.geoJson.features[0].geometry.coordinates.length
                  let lngLat = [[lngLatData[0][0], lngLatData[0][1]], [lngLatData[trackLen - 1][0], lngLatData[trackLen - 1][1]]]
                  for (let i = 0; i < urls.length; i++) {
                    this.addMarker(urls[i], lngLat[i], 28, 'top')
                  }
                  // 距离海拔数据
                  let distance = 0
                  this.chartData = [] // 清空数据
                  this.chartData.push([0, parseInt(lngLatData[0][2])])
                  for (let j = 1; j < lngLatData.length; j++) {
                    distance = distance + this.betDistance(lngLatData[j], lngLatData[j - 1])
                    this.chartData.push([parseFloat((distance / 1000).toFixed(2)), parseInt(lngLatData[j][2])])
                  }
                  // 增加各类航点图层数据源
                  this.addWaypointLayer(id)
                }
                // 最后一次循环,重置resizeMap
                if (m === this.mapTrack.length - 1) {
                  console.log('3新加载的路线为:' + m + '   ' + this.tracksId)
                  this.isReload = false
                  console.log('4更改状态为:' + this.isReload)
                }
              }
            })
          }
        },

    该怎么写呐?

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • ╰沒灵魂旳躯体 普通会员 1楼

      Promise和axios都属于HTTP库,它们的顺序可能会影响到用户使用它们的方式。

      Promise是一种编程模型,用于处理异步操作。它的优点在于,它可以确保在异步操作完成后立即返回结果,而不是像回调函数那样需要等待所有操作完成后再返回结果。

      axios是axios库的一个JavaScript API,用于发送HTTP请求。它的顺序是,首先发送GET请求,然后发送POST请求,最后发送PUT请求。这个顺序对于处理HTTP请求非常重要,因为异步操作可能会顺序地完成,而不仅仅是并行地完成。

      如果你想在异步操作完成后立即返回结果,你可以使用Promise。例如:

      javascript let promise = axios.get('https://api.example.com/data') .then(response => { // 处理响应 }) .catch(error => { // 处理错误 });

      如果你想在异步操作完成后使用axios发送其他请求,你需要在请求中使用axios。例如:

      javascript axios.get('https://api.example.com/data') .then(response => { // 处理响应 }) .then(data => { // 使用data处理响应 }) .catch(error => { // 处理错误 });

      因此,Promise和axios的顺序取决于你想要在何时返回结果。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部