How to call a second function after the execution of first

I have a function f1(valueOfData) executing which I can pull data from an external data source. Also, I have another function f2() which has an if loop and validates that the value has been pulled before calling a third function.
Having tried lots, I am stuck in a situation where in the second function(data Validation function) is getting called before the execution of f1(valueOfData) is complete and the data is pulled(takes a couple of minutes).

I understand that Javascript is event driven and hence tried using the second function(validation function) as a call back but however, I am still unable to achieve it. Also, I am unable to use the window.setTimeOut(millis) as I need to calculate the exact time taken to retrieve the data from the database (using

function call(){

function a(callBack){

Can somebody please help me suggest how to wait for the first function to entirely complete before executing the second.

42 thoughts on “How to call a second function after the execution of first”

  1. By default javascript runs codes sequentially. if you have synchronous functions you can call them like this:


    if it was async function you can simply use async await format:

    async function A(){
    await functionB();
    await functionC();

    or you can define callback function right after finishing the first one.


Leave a Comment