0

I am working with Esri ArcMap APIs and I made a function that makes query the layer and returns the result, I want to make this function return the result to use it in making widgets.

function queryLayer(filterType,value){

    var x 

    schoolLayer.definitionExpression = filterType+" = '" + value + "'";

    const queryParams = schoolLayer.createQuery();
    queryParams.where =  filterType +" = '" + value + "'";
    queryParams.outFields = [filterType]
    schoolLayer.queryFeatures(queryParams).then(function (results) {

        x = results.features
    });
   return x
}
  • [how do i return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – gaetanoM Sep 18 '18 at 10:48

1 Answers1

1

This is a promise, and promises are asynchronous.

schoolLayer.queryFeatures(queryParams).then(function (results) {
    x = results.features
});

You could to return your results inside your resolve-function:

function queryLayer(filterType,value){
    schoolLayer.definitionExpression = filterType+" = '" + value + "'";
    const queryParams = schoolLayer.createQuery();
    queryParams.where =  filterType +" = '" + value + "'";
    queryParams.outFields = [filterType]
    schoolLayer.queryFeatures(queryParams).then(function (results) {
        return results.features;
    });
}

If this does not work you could return the whole promise:

function queryLayer(filterType,value){
    schoolLayer.definitionExpression = filterType+" = '" + value + "'";
    const queryParams = schoolLayer.createQuery();
    queryParams.where =  filterType +" = '" + value + "'";
    queryParams.outFields = [filterType]
    return schoolLayer.queryFeatures(queryParams);
}

and use it like this:

queryLayer(filterType,value).then(funtction(response){
    // do whatever you want to do with your Response ...
}
jsadev.net
  • 2,800
  • 1
  • 16
  • 28