vue拦截器中再发起新的请求等请求完毕后的参数在进行判断是否执行下一步

这下面是大概的代码结构

function LocationAgain() {
    wx.ready(function() {
        //微信的地理位置获取方法
        wx.getLocation({
            type: 'wgs84',
            success: function(res) {
                Ajax().then((res) {
                    if (res.data.code == 0) {
                        return true
                    }

                })
            }

        })
    })
}
function Ajax() {
    return vm.$http.post(Url, {
        local: params
    })
}

Vue.http.interceptors.push((request, next) = >{

    if (request.headers.map.needlocal) {
        //用来判断是否需要获取地理位置
        LocationAgain() //希望在这里调用这个函数后得到返回后再进行下面的操作
    }

     next((response) = >{

});

});
     

因为中间有好几步操作都是异步的,我该怎么样才能得知Ajax()这个函数请求完毕后再决定要不要执行拦截器中后续的操作

阅读 2.9k
2 个回答

同上,我觉得用Promise可以的,在LocationAgain函数中套一层promise,大概这样

    function LocationAgain() {
        return new Promise(function(resolve, reject) {
            wx.ready(function() {
                //微信的地理位置获取方法
                wx.getLocation({
                    type: 'wgs84',
                    success: function(res) {
                        Ajax().then((res) {
                            if (res.data.code == 0) {
                                resolve();
                            }

                        })
                    }
                })
            })
        })
    }
    Vue.http.interceptors.push((request, next) = > {

        if (request.headers.map.needlocal) {
            //用来判断是否需要获取地理位置
            LocationAgain(()=>{
                //在这里调用这个函数后得到返回后再进行下面的操作
            }) 
        }

         
        next((response) = > {

        });
    });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题