一直無法理解什么為什么會(huì)在異步之后使用 resolve() 這個(gè)resolve是什么意思,直到看到了這個(gè)回答
https://www.imooc.com/qadetail/188492
Promise 對(duì)象代表一個(gè)異步操作,有三種狀態(tài):Pending(進(jìn)行中)、Resolved(已完成
,又稱 Fulfilled)和 Rejected(已失敗)。
通過回調(diào)里的resolve(data)將這個(gè)promise標(biāo)記為resolverd,然后進(jìn)行
下一步then((data)=>{//do something}),resolve里的參數(shù)就是你要傳入then的數(shù)據(jù)
return new Promise((omg, reject) => {
? console.log('this is esri.js');
? axios.get(configFilePath).then(function (configResponse) {
? ? console.log(configResponse);
? ? GLOABLE = configResponse.data;
? ? const options = {
? ? ? url: GLOABLE.esriApiUrl + "init.js"
? ? };
? ? // _this.attributeSubmitModel.uploadUrl = GLOABLE.fileServerUrl + "error_report/medias";
? ? loadCss(GLOABLE.esriApiUrl + "esri/css/main.css")
? ? loadModules([
? ? ? "esri/Map",
? ? ? "esri/WebMap",
? ? ? "esri/Basemap",
? ? ? 'esri/views/MapView',
? ? ? "esri/layers/TileLayer",
? ? ? "esri/layers/MapImageLayer",
? ? ], options)
? ? ? .then(([
? ? ? ? ? ? ? Map,
? ? ? ? ? ? ? WebMap,
? ? ? ? ? ? ? Basemap,
? ? ? ? ? ? ? MapView,
? ? ? ? ? ? ? TileLayer,
? ? ? ? ? ? ? MapImageLayer
? ? ? ? ? ? ]) => {
? ? ? ? _this.Map = Map;
? ? ? ? _this.WebMap = WebMap;
? ? ? ? _this.Basemap = Basemap;
? ? ? ? _this.MapView = MapView;
? ? ? ? _this.TileLayer = TileLayer;
? ? ? ? _this.MapImageLayer = MapImageLayer;
? ? ? ? omg()
? ? ? })
可以這樣理解,在新建 promise 的時(shí)候就傳入了兩個(gè)參數(shù)
這兩個(gè)參數(shù)用來標(biāo)記 promise的狀態(tài)的,這兩個(gè)參數(shù)是兩個(gè)方法,并且這兩個(gè)參數(shù)可以隨意命名,我這里的使用的是omg? 也不影響使用
用于表示 promise 的狀態(tài)
到執(zhí)行到 resolve()這個(gè)方法的時(shí)候,就改變promise的狀態(tài)為
fullfiled ,當(dāng)狀態(tài)為 fuulfiled的時(shí)候就可以執(zhí)行.then()
當(dāng)執(zhí)行到 reject()? 這個(gè)方法的時(shí)候,就改變 promise 的狀態(tài)為
reject,當(dāng) promise 為reject 就可以.catch() 這個(gè)promise了
然后這兩個(gè)方法可以帶上參數(shù),用于.then() 或者 .catch() 中使用。
所以這兩個(gè)方法不是替代,或者是執(zhí)行什么,他們的作用就是 用于改變
promise 的狀態(tài)。
然后,因?yàn)闋顟B(tài)改變了,所以才可以執(zhí)行相應(yīng)的 .then() 和 .catch()操作。