鍍金池/ 問答/HTML/ vue攔截器中再發(fā)起新的請求等請求完畢后的參數在進行判斷是否執(zhí)行下一步

vue攔截器中再發(fā)起新的請求等請求完畢后的參數在進行判斷是否執(zhí)行下一步

這下面是大概的代碼結構

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()這個函數請求完畢后再決定要不要執(zhí)行攔截器中后續(xù)的操作

回答
編輯回答
赱丅呿
2017年8月28日 12:02
編輯回答
敢試

同上,我覺得用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) = > {

        });
    });
2018年7月25日 20:44