1

我是 js 和 async/await 的新手,我想使用 yandex maps api 构建距离矩阵,然后使用 ACO 算法优化路线。一切正常,但是由于等待循环中的每个请求,我的距离矩阵生成了这么长时间。我知道我应该避免它,但我不知道怎么做。

我的距离矩阵应该在调用 ACO 算法函数之前完成。

async function buildDistanceMatrix(ymaps) {
  const n = routeCoords.length;
  let distanceMatrix = [];
  console.log(routeCoordsRef.current);

  for (let i = 0; i < n; i++) {
    let newArr = [];
    for (let j = 0; j < n; j++) {
      await ymaps.route([routeCoordsRef.current[i], routeCoords[j]]).then((route) => {
        newArr.push(route.getLength())
        console.log(i, j, routeCoordsRef.current[i], routeCoords[j], route.getLength());
      });
    }
    distanceMatrix.push(newArr);
  }
  return distanceMatrix;
}


let distanceMatrix = await buildDistanceMatrix(ymaps);
// and here my distance matrix done and i'm calling another function that uses distanceMatrix
4

1 回答 1

0

我认为您需要考虑以下两个注意事项...

注意 1: 通常在处理 Promise 时,要么使用“async”/“await”,要么坚持使用“.then()”语法,但不要混合使用它们。例如...

async function buildDistanceMatrix(ymaps) {...

    let route = await ymaps.route( [ routeCoordsRef.current[ i ], routeCoords[ j ] ] );
    newArr.push( route.getLength() );
    console.log( i, j, routeCoordsRef.current[ i ], routeCoords[ j ], route.getLength() );

...}

...或者...

function buildDistanceMatrix(ymaps) {...

    ymaps.route( [ routeCoordsRef.current[ i ], routeCoords[ j ] ] ).then( ( route ) => {
        newArr.push( route.getLength() );
        console.log( i, j, routeCoordsRef.current[ i ], routeCoords[ j ], route.getLength() );
    } );

...}

一般来说,“async”/“await” 更具可读性,因为它提供了一种按程序读取的语法,并有助于避免then()函数的嵌套地狱。

注意 2: Yandex 路由器功能似乎使用了 Web 服务......

...因此需要承诺,因为一旦 Yandex 服务器响应,javascript 代码本质上会暂停等待承诺解决。

建议查看Promise.all()函数...

...因为这允许您的函数启动许多承诺,而无需等待任何承诺完成,并且只有在创建所有承诺之后,您的函数才会等待所有承诺解决。

主要好处是远程服务器同时处理所有请求。

在沿着这条路径运行之前,请检查 Yandex 服务条款以确定是否对并发未完成呼叫的数量有限制。此外,设计良好的 Web 服务会限制并发请求的数量,因此对于少量 Web 调用可能会有一些性能上的大幅提升,但是在一定的限制之后,Web 服务器会启动,然后调用基本上会排队(或忽略!)并再次同步处理......

编辑:使用 Promise.all 和 async / await 语法。

简而言之,您的函数在创建 Promise 时不应等待,而是将 Promise 捕获到一个数组中。然后,一旦创建了所有的 Promise(在这种情况下,启动一系列 Web 服务调用),然后在Promise.all()函数上等待,传递所有未解决的 Promise。结果数组将直接对应于传入承诺的顺序Promise.all()。这通常是您的函数的外观。注意添加了parallelArray,这是一种在以后使用arrayOfResults时捕获您需要的任何辅助数据的方法......

async function buildDistanceMatrix(ymaps) {...

    let arrayOfPromises = [];
    let parallelArray = [];

    for ( let i = 0; i < n; i++ ) {
        for (let j = 0; j < n; j++ ) {
            arrayOfPromises.push( ymaps.route( [ routeCoordsRef.current[ i ], routeCoords[ j ] ] ) );
            parallelArray.push( { i: i, j: j, rci: routeCoordsRef.current[ i ], rcj: routeCoords[ j ] );
        }
    }

    let arrayOfResults = await Promise.all( arrayOfPromises );

...}

希望这可以帮助...

于 2020-12-17T01:18:24.087 回答