Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript – get one function to run after another

I have a function that runs when a click event happens on a button – first(). Inside it populates a dictionary, and second() uses those values in the dictionary. I want second() to run after that dictionary has been populated. I have tried to put second() in a callback inside first() but it runs synchronously, meaning, (i think), the dictionarys are still empty when it starts the second function. I think that I need first to return a promise, or get second function to await the finish of first function, but I cannot figure out how. Any guidance is appreciated. Also I want to do this without JQuery (Ideally).

function first(){
  populateDict()
}

function second(){
  loopThroughDict()
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

To understand async await, you can refer to this article

Here second() function will be called only after populateDict() returns something

async function first(){
  await populateDict();
  second();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading