在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

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

這下面是大概的代碼結(jié)構(gòu)

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() //希望在這里調(diào)用這個(gè)函數(shù)后得到返回后再進(jìn)行下面的操作
    }

     next((response) = >{

});

});
     

因?yàn)橹虚g有好幾步操作都是異步的,我該怎么樣才能得知Ajax()這個(gè)函數(shù)請(qǐng)求完畢后再?zèng)Q定要不要執(zhí)行攔截器中后續(xù)的操作

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

同上,我覺得用Promise可以的,在LocationAgain函數(shù)中套一層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(()=>{
                //在這里調(diào)用這個(gè)函數(shù)后得到返回后再進(jìn)行下面的操作
            }) 
        }

         
        next((response) = > {

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