Javascript Promise and await

Javascript Promise and await

conceptual understanding of execution context in JavaScript

Async | Promise | await:

  • Promise: In real life, we give assurance to each other for taking certain actions in future. In Javascript, A promise is an object which ensures to produce value in the future. it is used to manage to tackle asynchronous operations.

Promise Workflow-

  • pending: The pending promise is neither rejected nor fulfilled yet.

  • fulfilled: The related promise action is fulfilled successfully.

  • rejected: The related promise action failed to be fulfilled.

  • settled: Either the action is fulfilled or rejected.


Async: Instead of immediately returning the promised value it created a certain time to return the final value.


Await: This operator is used to wait for the promised value to get fulfilled and return the value.

Callback & HOF

  • Higher-order functions are functions which take other functions as the parameter and return functions as values. The function passed as a parameter called the callback

  • Hoisting: Declaring the variable before the execution of it while scanning it is called hoisting.

  • Spread Operator:
    It allows you to spread elements of an iterate object such as in arrays or maps.

  • Closure:
    The closure is the combination of the function which is bundled together. It gives access to an outer function from an inner function

  • Lexical scope:
    passing the parent function to the child and from the child function to its sub-child, as a parameter called lexical scope. Where the inner function has the access to the outer function variable which has been declared.

  • Global Scope:
    In the global function, the declaration of the variable can be called anywhere outside the function.

  • Local Scope:
    In Local scope whatever the variable is being declared would be in the inner function itself.

  • Extension context: Whenever the function is invoked, it creates a new execution context, There are two types of execution context

    1. Global Execution: The global context is always running, In the case of the browser it only gets stopped when we close the browser.
    2. Function Execution Context: When we call the function we place the current execution context on top of the global context, Because javascript is a single thread language It does things one at a time so when the function finishes it gets popped off from the stack and the previous function get paused. This is how it keeps the track of the position which is called the call stack

  • Call stack: The mechanism for the interpreter to keep the track of its place in a script that calls the multiple functions, The function being run and what function is called within the function. When the script calls the function the interpreter adds it to the call stack and carries out the function.

  • scope chain: The scope chain is the way to link all the variables of functions and other functions of the current execution context.

Spread-Operator:
When we try to pass multiple data to the function we use the spread operator. The spread operator enumerates the properties of the object and adds the key-value pairs to the object being created.

Rest:
Rest syntax is the opposite of the spread it condenses multiple elements into a single element.

Try-Catch:

Try-and-catch is comprised of a try block or a catch block for what particular code we are implementing if it gets exceptions or errors then it will throw an error we call to set the catch block in order to execute the error message before the control flow exits of the construct.

Did you find this article valuable?

Support Siddharth verma by becoming a sponsor. Any amount is appreciated!