0

Is it possible to monitor the JS function and identify when it's completed? Example: I have a JS method that has a long process time, I would like to monitor the process or at least know when it's done.

1 Answer 1

0

There are a few ways to know when a function has finished running.

Synchronous Functions

If the function in question runs completely synchronously, then the code which called the function will wait until it is done before executing. For example:

function a() {
    // do something
}

function b() {
    a();
    console.log("a is done"); // will only run once a is finished
}

Asynchronous Functions

Promises in JavaScript are another way to do this task, and they work even when you have asynchronous calls - e.g. requests to a server, etc. To get started, you make the first function return a promise:

function a() {
    return new Promise((resolve, reject)=>{
        // do something
        resolve(); // once we're done, resolve the promise
    });
}

The promise's constructor takes a function which takes two arguments, resolve and reject, which are given to it by JavaScript. You can call the resolve() function when you are done with processing, and the reject() function if you encounter some error which means you are unable to resolve.

To use this, you can do something like so:

function b() {
    a().then(()=>{
         console.log("a is done"); // will only run once a is finished
    });
}

The nice thing about running a promise is that all other code can continue running - nothing is blocked behind the promise except the anonymous function which you pass into the then method. This is definitely the approach I would take for something like this.

Monitoring progress / logging

With regards to monitoring the progress of a function, it really depends on how the function works. Your best bet is probably to make calls to some sort of logging function whenever the long function reaches certain parts. For example:

function a() {
    // do something
    console.log("did something");
    // do something else
    console.log("done");
}

If you can't change function a() for whatever reason, there's no good way to do this (that I'm aware of), unless you know it changes some global variable and you can check that for changes, but an approach like that would be quite dodgy and definitely not a good way to do things.

Conclusion

There's not too much more that I can really say without knowing what kind of function / process it is that you want to monitor - there are special ways to check on file upload progress, for example, that I have not covered here.

Likewise, if the process is happening on a different server or device, these methods won't work at all - you'd likely need to check out WebSockets or (more likely) Server Sent Events to get progress updates for those tasks.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.