在JavaScript函数中使用return的问题

时间:2018-09-18 10:42:33

标签: javascript function api esri

我正在使用Esri ArcMap API 然后我做了一个查询图层并返回结果的函数, 我想让此函数返回结果以在制作小部件时使用它。

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
}

1 个答案:

答案 0 :(得分:1)

这是一个承诺,并且承诺是异步的。

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

您可以在您的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;
    });
}

如果这不起作用,您可以退还全部承诺:

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

并像这样使用它:

queryLayer(filterType,value).then(funtction(response){
    // do whatever you want to do with your Response ...
}