How to use variables for Promise.all with REST API

Hello I'm currently facing an issue with asynchronous execution in JavaScript. I have an array (aaa) containing values [1, 2, 3, 4], and I'm using a loop to iterate through each value and set it using variable1.setValue(v). After setting the value, I immediately log the current value using console.log(variable1.value).

However, the problem is that the console.log(variable1.value) seems to output 4, 4, 4, 4 instead of the expected 1, 2, 3, 4. It appears that the asynchronous nature of variable1.setValue(v) might be causing the issue.

Here's the code snippet:

const aaa = [1, 2, 3, 4];

const processValues = async () => {
  for (const v of aaa) {
    await variable1.setValue(v);
    console.log(variable1.value);
  }
};

processValues();

image

Yes, as you've found, you can't set & get variable1.value multiple times in the same Javascript query.
What do you use variable1 for? maybe we can suggest a different way of doing it than setting it multiple times in the same block.

Hello, I have a question about executing Promise.all in Retool. I've configured the JavaScript as shown in the picture, but I'm unsure what values to provide in the rest API body. Can you provide guidance on this?

스크린샷 2023-12-01 125510
image

@cmlee: Yes, the way you trigger the query looks correct to me. It's correct to use additionalScope like that (just ignore the error message in the query UI). Are you having issues when using the code above?

1 Like

I received the error 'rest api resource does not have additionalScope field' before and gave up, but after your reply, something that didn't work suddenly started working. Are you a wizard? Thank you so much!

1 Like

Are you a wizard?

@cmlee How do you know? My name is inspired by Harry Potter hahah