• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JS Promises, Async and await

 
Ranch Hand
Posts: 94
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Paulson wrote:my question is how does it know to return the wakeup function as a new promise and not the log function? (or both?)


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

The promise has an executor function which is called and can either resolve (by calling resolve() or reject (by calling reject() the promise. (Note that these functions are passed to the executor function and can be named anything, but calling them anything but resolve and reject is to spit in the face of convention.)

If the promise is resolved, whatever is passed to resolve() becomes the result of the promise, in this case true (which is apparently discarded).

I am amusing [sic] 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).


No. As stated above, it returns a promise, not the function. The code has no access to the executor function.

The anonymous function created inside the executor function is stored in the global variable (boo! hiss! growl!) wakeup for later usage.

   await wakeable(); // returns to event loop here


This is likely the line thats could be generating confusion. wakeable is a function that creates and returns a promise. It does not return a function.

Be aware that await is just some syntactic sugar around dealing with promises without the need for callbacks.

That help?
 
Sean Paulson
Ranch Hand
Posts: 94
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Is the confusion about where the resolve function comes from? It's provided by the Promise interface and passed to the executor function as a parameter.
 
Sean Paulson
Ranch Hand
Posts: 94
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



When I look at the code (ignoring the comment), it's pretty clear to me that it does exactly that. Apart from also calling the log function before and after, that is. I agree that the comment isn't all that clear.
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Paulson wrote:
really? in his code comments it states


The comment is crappy.

To make wakeup alias resolve, the code would be: wakeup = resolve;
 
Paul Clapham
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sean Paulson
Ranch Hand
Posts: 94
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sean Paulson
Ranch Hand
Posts: 94
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



If I'm following this thread correctly, this is the central point of Sean's confusion.

If I look at that code with my Java hat on, then "wakeup" is a variable, and every time the "new Promise" constructor runs, a new value is assigned to that single variable. But this isn't Java. So there's some mechanism which makes the value of "makeup" belong to the newly-created instance of Promise rather than belonging to some external code. I haven't written JavaScript since before functional programming really took over so I can't explain this mechanism.
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Paul Clapham wrote:But this isn't Java. So there's some mechanism which makes the value of "makeup" belong to the newly-created instance of Promise rather than belonging to some external code.


Not really. The value of wakeup (a function) is passed a reference to a another function object (promise1.resolve). That reference won't change if you create a new Promise. This works the same way that Java works, except wakeup must be an instance field for it to be modifiable from within a lambda:

As you can see, the doTask() method gets a reference to a Consumer that belongs to the Promise instance that is created in the createTask() method. The consumer is passed-by-value, so the Runnable that is created inside the doTask() method will always call the consumer that belongs to the particular Promise instance that called the doTask() method.
 
Sean Paulson
Ranch Hand
Posts: 94
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bear Bibeault
Sheriff
Posts: 67754
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Paulson wrote:That makes sense...this code seems like it would be reckless to use though.


Not the best code I've seen. At minimum wakeup should be scoped.
 
reply
    Bookmark Topic Watch Topic
  • New Topic