All TalkersCode Topics

Follow TalkersCode On Social Media

AngularJS Wait For Function To Finish

Last Updated : Jul 1, 2023

AngularJS Wait For Function To Finish

In this article we will show you the solution of angularjs wait for function to finish, an asynchronous function returns a Promise, to which callback methods may be added based on the previous function's results.

A queue execution method executes functions sequentially as if they were arranged in a chain. In this case, functions following the previous function will have to wait in the queue for the result of the previous function.

Step By Step Guide On AngularJS Wait For Function To Finish :-

In the following sections, we will describe two popular options for doing that.

With setTimeout(), a function can wait for a promise to complete before returning it. For example, if the function returns a promise after a few milliseconds, then the promise will have finished.

SetTimeout() can be used with the async or await functions if you can't specify the exact timeout. With the async keyword, you can create an asynchronous function that returns a promise that can either be resolved or rejected.

Exceptions thrown by that function are rejected when the promise is rejected or are otherwise dealt with. Waiting for a promise using the await keyword pauses the execution of an async function.

Const wait=ms=>new Promise(resolve => setTimeout(resolve, ms));
function failureCallback(){
console.log("This is failure callback");
wait(4*1000).then(() => {
console.log("waited for 4 seconds");
throw new Error("error occurred");
}).catch(() => {
wait(2*1000).then(() => console.log("waited for 2 seconds"));
  1. Then we can be added to an HTML page with a <script> tag. The script tag explains the source code we used for the angularjs google API run and the file we used for the code.
  2. To wrap setTimeout in a promise returned by a future. We can wrap setTimeout in a promise by using the then() method to return a Promise.
  3. Then we use the callback function.
  4. Then we use the wait function. We use Basically, the promise constructor takes an executor function that lets us resolve or reject a promise manually. Since setTimeout() doesn't really fail, we left out reject in this case.
  5. We will simply create a try-catch block inside which we will perform our error-throwing and error-handling process.
  6. After that script closes using </script>

Conclusion :-

The get method now returns an instance of the Promise object. A promise expects to receive a function as a parameter, and it will pass resolve and reject functions to it as parameters.

When a promise is initialized, it is in the pending state. The resolve function is called if a request is completed successfully and changes the promise's state to fulfilled.

I hope this article on angularjs wait for function to finish helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪