✅ – Pink Hat – 00-54 Feb 15

PHPink Hat2/15/2023
in this code
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])
    
}

why does this.pattern[2] get printed 3 times?
> 0
> {bulletDelay: 1000, method: ƒ}
> 1
> {bulletDelay: 1000, method: ƒ}
> 2
> {bulletDelay: 1000, method: ƒ}
> undefined
> 3
> 3
> 3

When it should be printing
> 0
> {bulletDelay: 1000, method: ƒ}
> 1
> {bulletDelay: 1000, method: ƒ}
> 2
> {bulletDelay: 1000, method: ƒ}
> undefined
> 1
> 2
> 3
SScriptyChris2/15/2023
Try caching this.j as a local variable
PHPink Hat2/15/2023
caching it?
SScriptyChris2/15/2023
Yes, similarly as you do with i regarding this.i
PHPink Hat2/15/2023
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])
    
}
SScriptyChris2/15/2023
}(this.i, 0)) -> }(this.i, this.j))
PHPink Hat2/15/2023
nothing
PHPink Hat2/15/2023
still the same result
SScriptyChris2/15/2023
Can you add labels to console.log so it's more readable, which log refers to what value?
PHPink Hat2/15/2023
sure
SScriptyChris2/15/2023
console.log('i:', i)

console.log('this.pattern[this.i]:',this.pattern[this.i])
SScriptyChris2/15/2023
And show output
PHPink Hat2/15/2023
SScriptyChris2/15/2023
Where's that "3" log coming from? Add it label too
PHPink Hat2/15/2023
the 3 is the label
PHPink Hat2/15/2023
it was originally this
PHPink Hat2/15/2023
PHPink Hat2/15/2023
then so I could run it in chrome
PHPink Hat2/15/2023
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
        
    }  
]
PHPink Hat2/15/2023
Its a result of this.pattern[2] being run 3 times
PHPink Hat2/15/2023
I think it's happening because this.i is already fully changed before the setTimeout occurs
SScriptyChris2/15/2023
Can you add counters to 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,
    }  
]
SScriptyChris2/15/2023
Show the console output - i suppose each pattern will have counter: 3
SScriptyChris2/15/2023
Yes
SScriptyChris2/15/2023
You could await timeout call
PHPink Hat2/15/2023
in the for function?
SScriptyChris2/15/2023
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])   
}
PHPink Hat2/15/2023
would that work?
SScriptyChris2/15/2023
It should
PHPink Hat2/15/2023
ok. I'll try it
SScriptyChris2/15/2023
If this for..in is inside a function, then you have to make it async
SScriptyChris2/15/2023
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 promise
PHPink Hat2/15/2023
and inside the .then you would put what exactly?
PHPink Hat2/15/2023
because it seems the await doesnt have anything referencing it
SScriptyChris2/15/2023
Your console.log
PHPink Hat2/15/2023
ok.
SScriptyChris2/15/2023
What do you mean?
PHPink Hat2/15/2023
nvm that
PHPink Hat2/15/2023
so
PHPink Hat2/15/2023
PHPink Hat2/15/2023
now it's worse
PHPink Hat2/15/2023
it doesnt even wait the time it needs to
SScriptyChris2/15/2023
No, you have to wrap console.log inside a function, which you pass to .then()
SScriptyChris2/15/2023
.then(), same as setTimeout() or Promise() expects a function to be passed, which is then called inside
PHPink Hat2/15/2023
huh?
PHPink Hat2/15/2023
what do u mean by this
PHPink Hat2/15/2023
im confused as hell
SScriptyChris2/15/2023
Read example in the docs
SScriptyChris2/15/2023
Passing a function
SScriptyChris2/15/2023
.then((value) => console.log( /* stuff */ ))
PHPink Hat2/15/2023
I want the promise to pass after a certain time.
if I do
new Promise(resolve).then(() => console.log(/* stuff */)

then what does that do
SScriptyChris2/15/2023
What resolve is there?
SScriptyChris2/15/2023
I already wrote you example with promisified setTimeout above
SScriptyChris2/15/2023
..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]) 
PHPink Hat2/15/2023
threw this error (after I fixed some of the syntax cus it was complaining
SScriptyChris2/15/2023
Lacking closing curly brace
SScriptyChris2/15/2023
Add } there
SScriptyChris2/15/2023
And the console.log('what') is incorrectly place there
SScriptyChris2/15/2023
Just copy-paste this ☝️
SScriptyChris2/15/2023
Works for me on dry-run
PHPink Hat2/15/2023
it works
SScriptyChris2/15/2023
👍
PHPink Hat2/15/2023
I found a piece of code that works (slightly waltered from yours)
PHPink Hat2/15/2023
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]) 
});
}

and now we can close this. thanks for the help @ScriptyChris
PHPink Hat2/15/2023
#thanks
SScriptyChris2/15/2023
You may react to helpful answer with ✅ to mark thread as solved
UUUnknown User2/15/2023
3 Messages Not Public
Sign In & Join Server To View