I am trying to get data from rest service using 'esri/request'. My question is how to get response data from this request and use it outside of this in other function. But do not realize how it's possible under below fromOutSide() without using window variable.
https://developers.arcgis.com/javascript/3/jsapi/esri.request-amd.html
Finally found way to get variables from outside of .then using lang.hitch Code in below one
define([
'dojo',
'dojo/_base/declare',
'jimu/BaseWidget',
'dojo/_base/array',
'dojo/_base/lang',
'esri/request'
],
function(
dojo,
declare,
BaseWidget,
array,
lang,
esriRequest,
) {
// Run
this.getMapService;
getMapService: function() {
var requestHandle = esriRequest({
url: "https://services.arcgisonline.com/arcgis/rest/services/World_Topo_Map/MapServer/0?f=pjson",
content: {
f: "json"
},
handleAs: "json",
callbackParamName: "callback"
});
requestHandle.then(this.requestSucceeded, this.requestFailed);
},
requestSucceeded: function(response, io) {
let responseVar = response;
console.log(responseVar);
},
requestFailed: function(error, io) {
console.log(error);
},
fromOutSide: function() {
// ? ? ? ?
console.log(myVar);
}
});
Updated code: After lang.hitch I was able to use my variables inside .then
=========================================================
define([
'dojo/_base/lang',
'esri/request'
],
function (
lang,
esriRequest
) {
return declare([BaseWidget], {
getWindDirection: function () {
this.requestHandleNode.innerHTML = "";
requestArgs = {
url: this.config.windDirRestUrl,
content: {
f: "json"
},
handleAs: "json",
callbackParamName: "callback",
};
esriRequest(requestArgs).then(lang.hitch(this, function (response) {
this.requestSucceeded(response);
}),
lang.hitch(this, function (error) {
this.requestFailed(error);
})
);
},
requestSucceeded: function (response, io){
this.showWindDirection(response);
},
requestFailed: function (error){
console.log(error);
},
showWindDirection: function (response){
console.log(response)
}
});
});