Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Why are my method calls being executed in reverse order?

 
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 2 methods, but the handleLogin() method is being executed first even though I put it at the bottom.   On the console.log, it prints the last line first.    

 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was actually able to solve it by adding the setTimeout call.  

 
Marshal
Posts: 3861
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what your code looks like with proper formatting:
The handler code will not be executed until the Observable publishes by calling your next callback function.  It is very likely that the handleLogin function will be executed before that happens.

Did you mean to put the call to handleLogin inside the callback function?

Edit: fixed some grammar
 
Ron McLeod
Marshal
Posts: 3861
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:I was actually able to solve it by adding the setTimeout call.  


You worked-around the problem, and only if the server providing the data returns within 1000ms.
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:This is what your code looks like with proper formatting:
The handler code will not be executed until the Observable publishes by calling your next callback function.  It is very likely that before the handleLogin will be executed before that happens.

Did you mean to put the call to handleLogin inside the callback function?



Can you tell me what you mean by callback function?     I was able to solve it with the setTimeout function at 1000 ms.    For some reason, the code was being executed before everything inside the subscribe was finished until I added the timeout.
 
Ron McLeod
Marshal
Posts: 3861
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you subscribe to an Observable, you provide up to three callback functions:
   next: called when there is new data
   error: called when an error has occurred
   complete: called when there is no more data

The next callback is required; the other two are optional.

Your next callback function is:
 
Ron McLeod
Marshal
Posts: 3861
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably want to call the handleLogin function from within your next callback function:
 
Nathan Milota
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:You probably want to call the handleLogin function from within your next callback function:



That works just fine.  Thank You.
 
Ron McLeod
Marshal
Posts: 3861
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explanation here: Using observables to pass values
 
Ron McLeod
Marshal
Posts: 3861
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:That works just fine.  Thank You.


You're welcome

Try to spend some time to understand why you initially had a problem, why the work-around with the timer seemed to solve the problem, and why this last change worked in this case.
 
Creator of Enthuware JWS+ V6
Posts: 3398
312
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations Nathan Milota,

Your question has made it to our Journal  

Have a Cow!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic