如何在异步调用中跟踪同步变量 [英] How to Track a Sync Variable in an Async Call

查看:74
本文介绍了如何在异步调用中跟踪同步变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用量角器来运行e2e测试用例.所有量角器调用都是异步调用.以下代码是量角器代码:

I am using protractor to run e2e test cases. All protractor calls are async calls. The below code is a protractor code :

 gridRow.then(function (rows) {
                for (var index = 0; index < rows.length; index++) {
                    console.log(index); -------------------- 1
                    rows[index].all(by.className('ui-grid-cell-contents ng-scope')).get(1).getText().then(function (text) {
                        console.log(index); ----------------- 2
                        if (text.toUpperCase().replace(/ |-/g, '') === 'BRANCHONE') {
                            console.log(index); -------------- 3                              
                        }
                    });
                }
            });

在这种情况下,rows.length = 2.1处的Console.log按预期输出0和1,而2和3处的console.log输出为2,因为 then 调用是异步的.由于异步调用并行运行,因此,索引值更新为2(index ++).

The rows.length = 2 in this case. Console.log at 1 outputs 0 and 1 as expected but the console.log at 2 and 3 outputs as 2, because the then calls are async.Since the async calls run parallely , in the mean time the value of index gets updated to 2(index++).

关于如何在我的异步调用中跟踪该同步变量(索引)的任何想法?

Any idea about how to track that sync variale(index) inside my async calls?

推荐答案

我通常要做的是将回调函数包装在另一个函数中,以在新作用域中捕获sync变量.为了说明这一点,您现在所拥有的实际上是有效的:

What I usually do is wrap the callback function in another function to capture the sync variable in a new scope. To illustrate, what you have right now is effectively:

var callback = function(text) {
  // use text and index...
};
async_call(...).then(callback);

问题当然是创建回调后, index 变量(由回调函数捕获)会发生变化.由于回调函数使用 closure 来捕获对该值的引用,因此最终会看到更改后的值.

The problem is of course that the index variable (which is captured by the callback function) changes after the callback gets created. Because the callback function uses a closure to capture a reference to that value, it ends up seeing the changed value.

要在特定的循环迭代过程中捕获 index 的值,可以创建一个新的作用域,其中包含一个不变的新变量.基本上,您将创建另一个函数,并使用当前的 index 进行调用,该函数的参数将捕获该特定值.然后,您可以让该函数返回另一个用作回调的函数.清除该可怕描述的示例代码:

To capture the value of index during that particular loop iteration, you can create a new scope, with a new variable that doesn't get changed. Basically you create another function and call it with the current index, and the parameter for the function captures that specific value. Then you can have that function return another function that you use as your callback. Example code to clear up that horrible description:

function createCallback(curIndex) {
  return function(text) {
    // use text and curIndex...
  };
}

//...

var callback = createCallback(index); // where index is the variable you are looping over
async_call(...).then(callback);

可以通过使 createCallback 一个匿名函数更紧凑地编写:

This can be written more compactly by making createCallback an anonymous function:

var callback = (function(curIndex){
  return function(text) {
    // use text and curIndex...
  };
})(index);
async_call(...).then(callback);

有关JavaScript中的闭包和函数的更多信息,请参见此综合性答案.

For more information on closures and functions in JavaScript, see this comprehensive answer.

这篇关于如何在异步调用中跟踪同步变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆