# Await delay(1000) - error in array

**URL:** <https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589>\
**Category:** General\
**Created:** [25 October 2022 20:03 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589 "2022-10-25T20:03:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [25 October 2022 20:03 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/1 "2022-10-25T20:03:10Z")

</div>

Hi,  
I try to use await delay(1000) into this function

```auto
const delay = ms => new Promise(res => context.set("timer", setTimeout(res, ms)));

//x is an array
const x = msg.payload;

x.forEach(element => {
    const valore = (element.value)
    const id = (element.id)
    msg.topic = "UPDATE Variables SET Valore = '" + valore + "' WHERE Id = " + id
    msg.pass = 2
    node.status(valore + " " + id)
    node.send(msg)

    await delay(1000);
}
);

```

but I have this error:

 ![err](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/6960900d76b92176b5a1bf7bb8c89bcf43bd8aae.png)

How can I fix it?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 October 2022 20:23 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/2 "2022-10-25T20:23:40Z")

</div>

Have you tried the "quick fix" (hint, the arrow function must be async - just like the tip says)

That said...

This is NOT the node-red / low-code way. Get rid of the async await & simply send all your messages out of the function without delay then pass the msg through a delay node set to rate limit 1 msg per sec.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 October 2022 20:41 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/3 "2022-10-25T20:41:03Z")

</div>

And better still, create a more efficient SQL update statement that does it all in 1 statement.

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [26 October 2022 02:41 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/4 "2022-10-26T02:41:02Z")

</div>

Yes,  
I tried the "quick fix" and the function works but without delay ......

> [@Steve-Mcl](#):
>
> Get rid of the async await & simply send all your messages out of the function without delay then pass the msg through a delay node set to rate limit 1 msg per sec.

It's what I did until today, then I thought this method was better (have a break before instead after) .....

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [26 October 2022 02:42 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/5 "2022-10-26T02:42:31Z")

</div>

You're right, but this is the better that I can do ....

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 October 2022 05:38 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/6 "2022-10-26T05:38:24Z")

</div>

> [@Giamma](#):
>
> I tried the "quick fix" and the function works but without delay

Ah ok, yes. You cannot delay an array forEach. You would need to use a regular for loop

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 October 2022 05:58 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/7 "2022-10-26T05:58:11Z")

</div>

What @TotallyInformation says is a single statement.

Instead of looping one by one, create a single string with all the update statements and then execute it. This is extremely fast.

eg.

```auto
const input = msg.payload;
const output = ""

input.forEach(el => {
    output += `UPDATE Variables SET Valore = '${el.value}' WHERE Id = ${el.id};`
})

msg.topic = output
return msg

```

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [26 October 2022 06:14 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/8 "2022-10-26T06:14:58Z")

</div>

Thank you so much!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 October 2022 11:31 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/9 "2022-10-26T11:31:12Z")

</div>

Note that if your SQL DB supports them, prepared statements are typically FAR more efficient, especially for repeated updates and inserts.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 December 2022 11:31 UTC](https://discourse.nodered.org/t/await-delay-1000-error-in-array/69589/10 "2022-12-25T11:31:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
