zoukankan      html  css  js  c++  java
  • Promise的两种处理异步的方式

    单个异步处理:

    let usedMemoryPromise = fetchUsedMemeory();
    usedMemoryPromise.then(data => {...})
    
    
    
    
        function fetchUsedMemeory(){
            return new Promise( function(resolve){
                let clusterId = $("#cluster-id").val();
                invokeJsonRpc(RDS_CONFIG.APOLLO_CMDB_ENDPOINT_V3,
                    "query.aggregate",
                    {
                        graph: `graph g {  redisCluster: RedisCluster[ id = "${clusterId}" ];}`,
                        fields: [["redisCluster.name", "redisCluster.id", "redisCluster.used_memory"]]
                    }).then(resp => {
                    let usedMemory = resp.result.children[0].data["redisCluster.used_memory"];
                    resolve(usedMemory);
                })
            });
        }

    多个异步同时处理:

    async function findXdcrRelation(state, bucketId) {
        const xdcrSourcePromise = invokeJsonRpc(RDS_CONFIG.APOLLO_CMDB_ENDPOINT_V3,
            "query.resource",
            {
                graph: `g {
                        x: CouchbaseXDCR;
                        s: CouchbaseBucket [id=${bucketId}];
                        x  ~> @source_bucket ~> s;}`,
                target: 0
            });
        const xdcrTargetPromise = invokeJsonRpc(RDS_CONFIG.APOLLO_CMDB_ENDPOINT_V3,
            "query.resource",
            {
                graph: `g {
                        x: CouchbaseXDCR;
                        s: CouchbaseBucket [id=${bucketId}];
                        x  ~> @target_bucket ~> s;}`,
                target: 0
            });
        let [xdcrSourceData, xdcrTargetData] = await Promise.all([xdcrSourcePromise, xdcrTargetPromise]);
        const hasError = (xdcrSourceData.hasOwnProperty("error") && xdcrTargetData.hasOwnProperty("error"));
        if (!hasError && (xdcrSourceData.result.length > 0 || xdcrTargetData.result.length > 0)) {
            state.hasXdcrRelation = true;
        }
    }
  • 相关阅读:
    labVIEW学习——属性节点
    MATLAB学习
    WinForm中键盘按键问题
    TextBox的智能感知
    使用脚本操作UpdatePanel中控件的问题
    DBNull.Value,null,String.Empty之间的区别和联系
    Hotmail如何对已经存在的邮件地址进行更改?
    第一个hibernate项目
    html静态实现左边导航右边显示链接页面
    Struts2学习
  • 原文地址:https://www.cnblogs.com/jayinnn/p/10400822.html
Copyright © 2011-2022 走看看