Reading list Switch to dark mode

    Asynchronous functions in Javascript

    Updated 23 January 2019

    I this tutorial, I will discuss a brief about the async function in javascript. Recently I was working on a project where I met the requirement of the asynchronous functions in jquery and I am sharing that experience here. Before discussing the asynchronous functions let us see what do we mean by synchronous.

    Synchronous Code: 
    Synchronous code or synchronous programming means in sequence i.e statements execute in sequence. Each statement is executed one after the other i.e, a statement has to wait before the other one is finished. If you have three lines of code line1, line2, and line3, then line3 cannot execute until line2 has finished executing and line2 cannot execute until line1 has finished.

    Example:

    function synchronous_functions() {
       function1();
       function2();
       function3();
    }

    In the above example, function1 will be executed before function2 and function2 before fucntion3. The execution will start from top to bottom.

     

    Start your headless eCommerce
    now.
    Find out More

    Asynchronous Code(Async):
    Asynchronous code or asynchronous programming means in not in sequence i.e statements may not execute in sequence. Generally, we use asynchronous code to perform time-consuming operations. Async is the latest addition to the jquery.
    Async/await is a new syntax that allows us to compose Promises as though they were just normal synchronous functions without callbacks. It’s a fantastic addition to the Javascript language, added in 2017  in Javascript ES7, and can be used to simplify pretty much any existing JS application.

    The unique keyword is async before every declaration. Here is the following syntax:

    • Async function declarations: async function function1() {}
    • Async function expressions: const fun = async function2 () {};
    • Async method definitions: let obj = { async function1() {} }
    • Async arrow functions: const fun = async () => {};
    functionfunction1(ms) {
         return newPromise(ms);
    }
    async function asyncAwaitFunction () {
       const fun = await function1();
        console.log(fun);
    }
    

    Example:

    function timer(ms) {
        return new Promise(res => setTimeout(res, ms));
     }
     var len = 10;|
     async function load_slider () {
         for (var i = 0; i < len; i++) {
            console.log(i);
            await timer(5000);
          }
       }

    That is it.!!!
    If you liked this post, It would be very grateful if you write your opinions, comments and suggestions to keep the post updated and interesting.
    Thank you!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home