Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!

Sean Paulson

Ranch Hand
+ Follow
since May 19, 2015
Sean likes ...
Java
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
-1
Received in last 30 days
0
Total given
38
Given in last 30 days
4
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sean Paulson

Stephan van Hulst wrote:

Sean Paulson wrote:so wakeup is passed to the promise constructor?


No. wakeup is just a variable that all the code that you have posted can access. When you create a new Promise, the Promise constructor runs the executor function that you pass to it. That executor function assigns a new function object to the wakeup variable. This function object has a reference to the resolve function that is part of the Promise instance that you just created. If you create a second Promise, wakeup will be reassigned a new function object that now references the resolve function of the second Promise instance.



Ok so basically it's because wakeup is a global variable.

That makes sense...this code seems like it would be reckless to use though.
Thanks everyone I guess I was just over thinking it.

Stephan van Hulst wrote:What makes you think that the resolve function that is called inside the wakeup function would lose its link to the Promise that created it? If you call wakeup outside of the executor that you pass to the Promise constructor, what promise would there be to resolve other than the one that you created the wakeup function inside of?



so wakeup is passed to the promise constructor

Paul Clapham wrote:If this were real life, I would strongly suspect that the comment was written when the code was wakeup = resolve, and then logging was added to the code as shown but the comment wasn't changed.



makes sense. I guess I just dont understand how the resolve() in wakeup is still linked to the promise when called outside. I know this would not work but something like newPromise.resolve() would make sense to me.

Bear Bibeault wrote:

Sean Paulson wrote:
My confusion is with the wakeup() how is wakeup set to the resolve function?


It isn't. It's set to the anonymous function, which in turn calls the resolve function.



really? in his code comments it states
I think I just need to learn more about async await and come back to this.

btw here is the link to this example javascript Promises and await
finally able to get back to my computer.
thanks for the answer. No I am still a bit confused but I dont think its because of your explanation. I just need to mess around with promises more. (just started on them last week)




It doesn't return either function. The call to create the promise returns a Promise instance.



I misspoke I think. I realize it returns a new promise. (line 2)
line 4. wakeup is assigned a function that when called resolves the new promise.
My confusion is with the wakeup() how is wakeup set to the resolve function?

Can someone explain some of this code below.
Specifically the wakeable() function.
From my understanding it obviously returns a promise object.
I am amusing it is returning the wakeup function as a promise object to be precise(also keeping it in a unresolved state until the wakeup is called).
my question is how does it know to return the wakeup function as a new promise and not the log function? (or both?)


output: waiting…
(wakeable: creating Promise, setting wakeup to resolve function)
main: about to call wakeup
wakeup: Woke up!
wakeup: after resolve
Reached end of source file
handle_event: await returned OK!
waiting…
(wakeable: creating Promise, setting wakeup to resolve function)
wakeup: Woke up!
wakeup: after resolve
handle_event: await returned OK!
waiting…
(wakeable: creating Promise, setting wakeup to resolve function)
So I am following this tutorial and they use a object like this ..


Then they have a function "toggle" that takes the currentGameBoard obj and a random ordered pair between 0 and 2. It uses a clever little statement to find the corresponding index.
My question is: does this have a name? have you seen this before? or do you have any information you can direct me to.
I get how it works I just thought it was cleaver but I cant find any info on the web about it.

Tim Cooke wrote:The compiler is complaining because your class syntax is incorrect.



What he said. Unless you want some of those variables to be not be local variables then you can declare them before the constructor. Most of that code does need to be in a method though.
3 years ago

Miley Johnson wrote:

Why do I get these errors on compile:



You are probably missing a ending ; or a }, somewhere in the class. Also you should declare your variables and your array before the constructor.
3 years ago

Junilu Lacar wrote: I'm not inclined to lose more brain cells trying to figure out the correct structuring.


Well ok then
3 years ago

Junilu Lacar wrote:Then your main flow will no longer have a Single Level of abstraction



So then changing the while loop to this



does not count as single level abstraction?
Doing this I have almost half the code i did before in my Game class and a little less in my Board class.
3 years ago

Junilu Lacar wrote:
There are certainly opportunities to define more abstractions using an enum type.



Yeah I agree i will probably keep the classes the way they are. Also tighten up(get rid of some of the middle man functions) the while loop in the Game class.
Then have enums in class Board. enum gameStates WON, TIE, ENDINERROR;

For now I think i will just make a simple program and mess around with objects, enums and ect. To make sure I have a better grasp on everything.  
3 years ago

Sean Paulson wrote:
then the move() function would return col and row directly to the Board class, instead of returning row and col to the Game class first?



Well if this is true I didnt know I could do this. I think im just trying to move to fast and need to spend more time testing different subjects.
Such as what all can i do with objects and such.
3 years ago

Junilu Lacar wrote:




So board.isValidMove would first call player[i].move()

then the move() function would return col and row directly to the Board class, instead of returning row and col to the Game class first?
3 years ago