从异步函数返回 MapView 实例(或将其传递给承诺解析器)导致承诺无限链
Returning MapView instance from async function (or passing it to promise resolver) cause a promise infinite chain
下面的简单代码会产生数以千计的警告日志,如下所示:[esri.core.Promise] DEPRECATED: then() -- use .when(callback, errback) instead
。
简单代码:
async function test() {
const view = new MapView({
container: "map",
map: new Map({
basemap: "satellite"
}),
zoom: 2
});
return view;
}
whitout return view
一切正常。
将 MapView 实例传递给解析器时附加相同的内容,例如 Promise.resolve(mapViewInstance)
或 .then(() => mapViewInstance)
Somes ArcGIS JS API 类 看起来像承诺,因为它们的实例提供了 then()
方法。
见https://developers.arcgis.com/javascript/latest/guide/working-with-promises/#api-classes-as-promises
如果您 return 一个异步函数 类 的实例(或将其传递给 promise 解析器),promise 将认为它实际上是一个新的 promise 并将链接它,就好像你会做类似 .then(() => new Promise())
.
解决方法:
将实例包装在数组或对象中以 "hide" .then()
方法:
Promise.resolve([mapViewInstance])
Promise.resolve({mapViewInstance})
async function test() {
return [mapViewInstance]
}
初始反映:
下面的简单代码会产生数以千计的警告日志,如下所示:[esri.core.Promise] DEPRECATED: then() -- use .when(callback, errback) instead
。
简单代码:
async function test() {
const view = new MapView({
container: "map",
map: new Map({
basemap: "satellite"
}),
zoom: 2
});
return view;
}
whitout return view
一切正常。
将 MapView 实例传递给解析器时附加相同的内容,例如 Promise.resolve(mapViewInstance)
或 .then(() => mapViewInstance)
Somes ArcGIS JS API 类 看起来像承诺,因为它们的实例提供了 then()
方法。
见https://developers.arcgis.com/javascript/latest/guide/working-with-promises/#api-classes-as-promises
如果您 return 一个异步函数 类 的实例(或将其传递给 promise 解析器),promise 将认为它实际上是一个新的 promise 并将链接它,就好像你会做类似 .then(() => new Promise())
.
解决方法:
将实例包装在数组或对象中以 "hide" .then()
方法:
Promise.resolve([mapViewInstance])
Promise.resolve({mapViewInstance})
async function test() {
return [mapViewInstance]
}
初始反映: