简体   繁体   English

如何在Javascript的while循环中按顺序执行代码?

[英]How to execute code in order in a while loop in Javascript?

I'm trying to run some JavaScript code on Postman, but I can't find a way for it to run in the order that I need.我正在尝试在 Postman 上运行一些 JavaScript 代码,但我找不到按我需要的顺序运行的方法。 This is what I'm trying to do:这就是我想要做的:

  • Retrieve the API response and verify if the "pending" array contains any item检索 API 响应并验证“待处理”数组是否包含任何项目
  • If id does, I'll save the id of the record (orderId) in an environment variable to use in my actual request如果 id 存在,我会将记录的 id (orderId) 保存在环境变量中以在我的实际请求中使用
  • At this point I would set found = true and break the loop when it leaves the setTimout function此时我将设置 found = true 并在它离开 setTimout function 时打破循环

Note: I created the function to introduce 400ms delay between the attempts, as it will allow the pending array to be populated注意:我创建了 function 以在尝试之间引入 400 毫秒的延迟,因为它将允许填充待处理的数组

    var found = false;
    var counter = 0;

    while (counter < 10) {

        setTimeout(async () => {
            var size = await response.json().pending.length;

            if (size > 0) {
                var orderId = response.json().pending[0].orderId;
                pm.environment.set("current_order", orderId);
                found = true;
            }
        }, [400]);

        console.log(found);
        if (found) { break; }

        counter++;
    }

My problem is that the part that is outside the setTimeout function executes first, so it will never satisfy the condition "If (found)".我的问题是 setTimeout function 之外的部分首先执行,所以它永远不会满足条件“如果(找到)”。 It always executes the code 10 times, even if the record is found in the first attempt.即使在第一次尝试中找到记录,它也总是执行代码 10 次。

My question is: How can I write it in order to check if the record was found after each attempt and break from the loop if positive?我的问题是:我如何编写它以检查每次尝试后是否找到记录并在肯定时从循环中中断?

Thanks!谢谢!

As suggested above, you might be able to solve this issue with a simpler recursive function.如上所述,您可以使用更简单的递归 function 来解决此问题。 An example on how it would look:关于它的外观的示例:

var found = false;
var size = 0;

async function checkResponse() {
  var size = await response.json().pending.length;

  if (size > 0) {
    var orderId = await response.json().pending[0].orderId;
    pm.environment.set('current_order', orderId);
    found = true;
  }
  if (!found) {
    await checkResponse();
  }
}

await checkResponse();

Here is a synchronous test for the logic above: 下面是对上述逻辑的同步测试:
-4
END?
false
-3
END?
false
-2
END?
false
-1
END?
false
0
END?
false
1
pm.environment.set('current_order', orderId);
END?
true
checkResponse();
checkResponse();
checkResponse();
checkResponse();
checkResponse();

Output: Output:

 -4 END? false -3 END? false -2 END? false -1 END? false 0 END? false 1 pm.environment.set('current_order', orderId); END? true checkResponse(); checkResponse(); checkResponse(); checkResponse(); checkResponse();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM