一直无法理解什么为什么会在异步之后使用 resolve() 这个resolve是什么意思,直到看到了这个回答
https://www.imooc.com/qadetail/188492
Promise 对象代表一个异步操作,有三种状态:Pending(进行中)、Resolved(已完成
,又称 Fulfilled)和 Rejected(已失败)。
通过回调里的resolve(data)将这个promise标记为resolverd,然后进行
下一步then((data)=>{//do something}),resolve里的参数就是你要传入then的数据
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 的时候就传入了两个参数
这两个参数用来标记 promise的状态的,这两个参数是两个方法,并且这两个参数可以随意命名,我这里的使用的是omg 也不影响使用
用于表示 promise 的状态
到执行到 resolve()这个方法的时候,就改变promise的状态为
fullfiled ,当状态为 fuulfiled的时候就可以执行.then()
当执行到 reject() 这个方法的时候,就改变 promise 的状态为
reject,当 promise 为reject 就可以.catch() 这个promise了
然后这两个方法可以带上参数,用于.then() 或者 .catch() 中使用。
所以这两个方法不是替代,或者是执行什么,他们的作用就是 用于改变
promise 的状态。
然后,因为状态改变了,所以才可以执行相应的 .then() 和 .catch()操作。