R
Reactiflux
help-js
✅ – Pink Hat – 00-54 Feb 15
in this code
why does
When it should be printing
this.pattern = [
{
method: new Function(`console.log(1)`),
bulletDelay: 1000
} ,{
method: new Function(`console.log(2)`),
bulletDelay: 1000
} ,{
method: new Function(`console.log(3)`),
bulletDelay: 1000
}
]
for (this.i in this.pattern) {
setTimeout(()=>this.pattern[this.i].method(), function (i){
this.z = 0;
for (this.j = i-1; this.j >=0; this.j--) {
try {
this.z += this.pattern[this.j]?.bulletDelay
} catch (error) {
console.error(error)
} finally {
continue
}
}
console.log(i)
return this.z
}(this.i))
console.log(this.pattern[this.i])
}
this.pattern = [
{
method: new Function(`console.log(1)`),
bulletDelay: 1000
} ,{
method: new Function(`console.log(2)`),
bulletDelay: 1000
} ,{
method: new Function(`console.log(3)`),
bulletDelay: 1000
}
]
for (this.i in this.pattern) {
setTimeout(()=>this.pattern[this.i].method(), function (i){
this.z = 0;
for (this.j = i-1; this.j >=0; this.j--) {
try {
this.z += this.pattern[this.j]?.bulletDelay
} catch (error) {
console.error(error)
} finally {
continue
}
}
console.log(i)
return this.z
}(this.i))
console.log(this.pattern[this.i])
}
this.pattern[2]
get printed 3 times?
> 0
> {bulletDelay: 1000, method: ƒ}
> 1
> {bulletDelay: 1000, method: ƒ}
> 2
> {bulletDelay: 1000, method: ƒ}
> undefined
> 3
> 3
> 3
> 0
> {bulletDelay: 1000, method: ƒ}
> 1
> {bulletDelay: 1000, method: ƒ}
> 2
> {bulletDelay: 1000, method: ƒ}
> undefined
> 3
> 3
> 3
> 0
> {bulletDelay: 1000, method: ƒ}
> 1
> {bulletDelay: 1000, method: ƒ}
> 2
> {bulletDelay: 1000, method: ƒ}
> undefined
> 1
> 2
> 3
> 0
> {bulletDelay: 1000, method: ƒ}
> 1
> {bulletDelay: 1000, method: ƒ}
> 2
> {bulletDelay: 1000, method: ƒ}
> undefined
> 1
> 2
> 3
Try caching
this.j
as a local variablecaching it?
Yes, similarly as you do with
i
regarding this.i
like this?
for (this.i in this.pattern) {
setTimeout(()=>this.pattern[this.i].method(), function (i, j){
this.z = 0;
for (j >=0; j--) {
try {
this.z += this.pattern[j]?.bulletDelay
} catch (error) {
console.error(error)
} finally {
continue
}
}
console.log(i)
return this.z
}(this.i, 0))
console.log(this.pattern[this.i])
}
for (this.i in this.pattern) {
setTimeout(()=>this.pattern[this.i].method(), function (i, j){
this.z = 0;
for (j >=0; j--) {
try {
this.z += this.pattern[j]?.bulletDelay
} catch (error) {
console.error(error)
} finally {
continue
}
}
console.log(i)
return this.z
}(this.i, 0))
console.log(this.pattern[this.i])
}
}(this.i, 0))
-> }(this.i, this.j))
nothing
still the same result
Can you add labels to
console.log
so it's more readable, which log refers to what value?sure
console.log('i:', i)
console.log('this.pattern[this.i]:',this.pattern[this.i])
console.log('i:', i)
console.log('this.pattern[this.i]:',this.pattern[this.i])

Where's that "3" log coming from? Add it label too
the 3 is the label
it was originally this

then so I could run it in chrome
Its a result of
this.pattern = [
{
method: new Function(`console.log(1)`),
bulletDelay: 1000
} ,{
method: new Function(`console.log(2)`),
bulletDelay: 1000
} ,{
method: new Function(`console.log(3)`),
bulletDelay: 1000
}
]
this.pattern = [
{
method: new Function(`console.log(1)`),
bulletDelay: 1000
} ,{
method: new Function(`console.log(2)`),
bulletDelay: 1000
} ,{
method: new Function(`console.log(3)`),
bulletDelay: 1000
}
]
this.pattern[2]
being run 3 times
I think it's happening because this.i is already fully changed before the setTimeout occursCan you add counters to
Show the console output - i suppose each pattern will have
pattern
too?
this.pattern = [
{
method: new Function(`console.log(1)`),
bulletDelay: 1000 ,
counter: 1,
} ,{
method: new Function(`console.log(2)`),
bulletDelay: 1000,
counter: 2,
} ,{
method: new Function(`console.log(3)`),
bulletDelay: 1000,
counter: 3,
}
]
this.pattern = [
{
method: new Function(`console.log(1)`),
bulletDelay: 1000 ,
counter: 1,
} ,{
method: new Function(`console.log(2)`),
bulletDelay: 1000,
counter: 2,
} ,{
method: new Function(`console.log(3)`),
bulletDelay: 1000,
counter: 3,
}
]
counter: 3
Yes
You could await timeout callin the for function?
for (this.i in this.pattern) {
await new Promise(resolve => setTimeout(()=>this.pattern[this.i].method(), function (i, j){
// stuff
// `return this.z` becomes
resolve(this.z)
}(this.i, 0))
console.log(this.pattern[this.i])
}
for (this.i in this.pattern) {
await new Promise(resolve => setTimeout(()=>this.pattern[this.i].method(), function (i, j){
// stuff
// `return this.z` becomes
resolve(this.z)
}(this.i, 0))
console.log(this.pattern[this.i])
}
would that work?
It should
ok. I'll try it
If this
for..in
is inside a function, then you have to make it async
If it's in top-level scope, then your script should work as a module, otherwise await
won't work, and you have to .then()
the promiseand inside the .then you would put what exactly?
because it seems the await doesnt have anything referencing it
Your
console.log
ok.
What do you mean?
nvm that
so

now it's worse
it doesnt even wait the time it needs to
No, you have to wrap
console.log
inside a function, which you pass to .then()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#using_the_then_method
.then()
, same as setTimeout()
or Promise()
expects a function to be passed, which is then called insidehuh?
what do u mean by this
im confused as hell
Read example in the docs
.then((value) => console.log( /* stuff */ ))
I want the promise to pass after a certain time.
if I do
then what does that do
new Promise(resolve).then(() => console.log(/* stuff */)
new Promise(resolve).then(() => console.log(/* stuff */)
What
resolve
is there?
I already wrote you example with promisified setTimeout
above
..ok, try this - i think i misread your code (2:30am on my side)
await new Promise(
(resolve) => {
setTimeout(
() => {
this.pattern[this.i].method();
resolve();
},
function (i, j){
// stuff
return this.z;
}(this.i, 0)
)
}
)
console.log(this.pattern[this.i])
await new Promise(
(resolve) => {
setTimeout(
() => {
this.pattern[this.i].method();
resolve();
},
function (i, j){
// stuff
return this.z;
}(this.i, 0)
)
}
)
console.log(this.pattern[this.i])
threw this error (after I fixed some of the syntax cus it was complaining

Lacking closing curly brace
And the
console.log('what')
is incorrectly place there
Just copy-paste this ☝️it works
👍
I found a piece of code that works (slightly waltered from yours)
and now we can close this. thanks for the help @ScriptyChris
thanks
this.pattern = [
{
method: new Function('console.log("Counter: 1")'),
bulletDelay: 1000
} ,{
method: new Function('console.log("Counter: 2")'),
bulletDelay: 1000
} ,{
method: new Function('console.log("Counter: 3")'),
bulletDelay: 1000
}
]
for (this.i in this.pattern) {
await new Promise(
(resolve) => {
setTimeout(
() => {
this.pattern[this.i].method();
resolve();
},
function (i, j){
// stuff
return this.z;
}(this.i, 0)
)
console.log(this.pattern[this.i])
});
}
this.pattern = [
{
method: new Function('console.log("Counter: 1")'),
bulletDelay: 1000
} ,{
method: new Function('console.log("Counter: 2")'),
bulletDelay: 1000
} ,{
method: new Function('console.log("Counter: 3")'),
bulletDelay: 1000
}
]
for (this.i in this.pattern) {
await new Promise(
(resolve) => {
setTimeout(
() => {
this.pattern[this.i].method();
resolve();
},
function (i, j){
// stuff
return this.z;
}(this.i, 0)
)
console.log(this.pattern[this.i])
});
}
You may react to helpful answer with ✅ to mark thread as solved
3 Messages Not Public
Sign In & Join Server To View
Looking for more? Join the community!
R
Reactiflux
help-js
R
Reactiflux
help-js
Want results from more Discord servers?
Recommended Posts✅ – Pink Hat – 23-03 Feb 14What are the arguments in an arrow function when it is inside of a `setTimeout()` function
```js
setPink Hat – 22-12 Feb 13why do I get this error with this code?How to prevent parent element onClick event when child element outside click event triggered?Hey, everybody, I am trying to build custom pop-ups and/or dropdowns using React, and I need your he⛄Snowberb⛄ – 07-57 Feb 9Having an interceptor do redirect to the login page when a certain status is seen, how could I do toEduardS – 18-34 Feb 8How to solve calling hooks conditionally here?
I have this `data` coming from `validateData` that rHow to avoid calling hook conditionally here?I have this `data` coming from `validateData` that return the `rawData` if valid otherwise `null`. Endo – 07-14 Feb 8Does anyone know how to autoplay video with sound? I know there is a Autoplay policy. but for exampkairu – 14-23 Feb 7I have a section in my code I don't really understand.
`function nameOfFunction(a, b = null, c =