我在微信小程序中自定義兩個函數,一個是獲取位置,一個是獲取天氣,
在Page中
Page({ getLocalCity: function(){}, getWeather: function(){}, })
這樣定義函數的,
調用時這樣的:
Page({ onLoad:function(){ this.getLocalCity(); this.getWeather(); } })
在onLoad的時候調用,但我無論怎么寫,getWeather()函數都是執行在前,這是為什么呢?
return 一個 Promise 然后鏈式調用。
getLocalCity() { return new Promise(resolve => { wx.request({ url: "", success: res => { // ... return resolve(); }, }) }); }, getWeather(){ // ... }, onLoad() { this.getLocalCity().then(result => { this.getWeather(); }); }