• ramda-compose中包含异步操作


     

    在函数组合时,如果有异步操作,那么ramda中compose应该怎么写呢?

    在写空间查询例子的时候,就遇到了这个问题,其中doQuery方法是一个异步函数。

    解决方法是一个andThen函数。

    代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    // highLight上图
    let highLightHandler: __esri.Handle = null;
    const highLight = (features: Array<Graphic>) => {
    view.whenLayerView(featureLayer).then(layerView => {
    if (highLightHandler) {
    highLightHandler.remove(); // 清空高亮
    }
    highLightHandler = layerView.highlight(features);
    });
    }

    // 使用查询条件查询返回查询结果
    // @ts-ignore
    const doQuery = async query => {
    const result = await featureLayer.queryFeatures(query);
    const { features } = result;
    return features;
    }

    // 使用绘制的图形生成查询条件
    const generateQuery = (geometry: Geometry) => new Query({
    returnGeometry: true,
    outFields: ["NAME", "OBJECTID"], // highlight必须要OBJECTID字段
    geometry,
    outSpatialReference: view.spatialReference,
    spatialRelationship: "intersects"
    });

    // 查询结果展示到控制台
    const showInTable = (features: Graphic[]) => {
    const attributes = features.map(feature => feature.attributes);
    console.table(attributes);
    }

    // 处理查询结果
    const showQueryResult = (features: Graphic[]) => {
    showInTable(features);
    highLight(features);
    graphicsLayer.removeAll(); // 清空绘制图形
    }

    const workFlow = R.compose(
    R.andThen(showQueryResult), // 处理查询结果
    doQuery, // 使用查询条件查询返回查询结果
    generateQuery // 使用绘制的图形生成查询条件
    );

    // 2.在绘制结束,拿到绘制的图形
    sketch.on("create", event => {
    if (event.state === "complete") {
    const geometry = event.graphic.geometry;
    workFlow(geometry);
    }
    });
  • 相关阅读:
    SQL 表变量用法
    <a>标签内嵌<input type="image">在IE中链接失效问题
    jquery 关于table的子标签tbody
    调用系统存储过程清空所有表
    战争的十四行
    xx,我们一起跳西湖去
    28
    两个情境和一个梦
    从头学习compiler系列1——前言
    从头学习compiler系列2——COOL语言学习1
  • 原文地址:https://www.cnblogs.com/lihaijia/p/14556232.html
Copyright © 2020-2023  润新知