• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

How to finish a called function before continuing the calling function so returned data can be used?

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using jQuery and javascript to call a function within another to get required data (a list of Activities that relate to an Activity Type); however, the first function finishes before the second is started and returns its data. How do I stop the fist function until the second has returned its data? I have read a large amount of information and it seems that I need to use Promise; however, I can not figure out how to implement it AND Promise is not supported by Explorer which excludes my program from a large number of users. Can anyone please explain how I can convert my example please?

My code is:

 
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are already using a Promise whether you know it or not -- the function passed to .done() is executed when the promise is resolved.

Can you point out the two functions you are talking about? Except for the promise created higher up than the code you included, I don't see anything asynchronous going on.

Glyndwr Bartlett wrote:How do I stop the fist function until the second has returned its data?


As you have surmised, you don't. Rather, a promise is returned and callback functions are invoked when the promise is either resolved or rejected (or an error could be thrown and caught).

Again, you'll need to point out where the functions in question are within the code.
 
Glyndwr Bartlett
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

So I am in the ajax function that creates a row with the Activity Type and the associated list of Activities for the selected Activity Types:



This loops through the Activity Types creates a list of Activity Type options and marks the one that has been selected:



Cooking
Games (selected)

For the selected item I get a list of Activities:



And mark the one that has been selected:

Chess
Duck, Duck, Goose (selected)
Round Robin

So I need to finish the Activity Type and Activity pair be fore moving to the next (i+1) Activity Type and Activity pair.

Kind regards,

Glyn






Capture.PNG
[Thumbnail for Capture.PNG]
 
Bear Bibeault
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are using jQuery for your Ajax call. If so, what you want to do is to pass a function to newAction, that can be used in the .done() resolution function of the Ajax call. This function can perform whatever actions need to wait until the response is received.
 
Bear Bibeault
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. What customer base is still focused on Explore? It's been end-of-lifed for over 4 years.
 
Glyndwr Bartlett
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

This is the calling function:



And this is the called function:




How do I modify the calling and called functions please (this is what I did not understand in my readings)?

This does use jQuery. In my reading I came across the fact that Explorer does not support Promise. You say this is no longer relevant then?

Kind regards,

Glyn
 
Bear Bibeault
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Within newActivity you already have a .done() callback, so that's where you do anything that needs to wait for the response.
 
Glyndwr Bartlett
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

Sorry, I do not understand your reply. The done() does not wait for the call to the function "newActivity":



to return its response/data. How do I make the "done()" wait until the response from "newActivity" is returned before continuing on to the next line please?

Kind regards,

Glyn
 
Bear Bibeault
Marshal
Posts: 67478
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The .done() callback is in the function newActivity at line 27, so newActivity is already executing before the ajax call is even made.

At line 4 you make the ajax call, and when the response returns, the function passed to .done() at line 27 is executed, with the response data in parameter responseJson2a. It is within this callback function that any code that needs to wait for the response must be placed.

That better?
 
Glyndwr Bartlett
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

I am very sorry; however, I do not understand. I have been researching and found ".while". So I have changed my code to:




And moved all the row creation and attachment into the function "newActivity".

Please let me know if you see any flaw in this approach. I very much appreciate all your help.

Kind regards,

Glyn
 
reply
    Bookmark Topic Watch Topic
  • New Topic