Skip to main content

JS Concepts : Call backs,promise,async,await


Promises : Easy to write async code


















function loadScript(src, callback) {
  let script = document.createElement('script');
  script.src = src;

  script.onload = () => callback(script);

  document.head.append(script);
}
loadScript('/my/script.js', function() {
  // the callback runs after the script is loaded
  newFunction(); // so now it works
  ...
});


PYRAMID OF DOOM 

















The “pyramid” of nested calls grows to the right with every asynchronous action. Soon it spirals out of control.




Solving pyramid of doom by promise chaining
-

-


ASYNC

The word “async” before a function means one simple thing: a function always returns a promise. If the code has return  in it, then JavaScript automatically wraps it into a resolved promise with that value.


The keyword await makes JavaScript wait until that promise settles and returns its result.

-

-

What is Promise ? When to use Promise API methods ?

The constructor syntax for a promise object is:
let promise = new Promise(function(resolve, reject) {
  // executor (the producing code, "singer")
});
The function passed to new Promise is called executor. When the promise is created, it’s called automatically. It contains the producing code, that should eventually finish with a result. In terms of the analogy above, the executor is a “singer”.
The resulting promise object has internal properties:
  • state – initially is “pending”, then changes to “fulfilled” or “rejected”,
  • result – an arbitrary value, initially undefined.
When the executor finishes the job, it should call one of:
  • resolve(value) – to indicate that the job finished successfully:
    • sets state to "fulfilled",
    • sets result to value.
  • reject(error) – to indicate that an error occurred:
    • sets state to "rejected",
    • sets result to error.
-
-


Promise API methods  (In progress )

  • Resolve
  • Reject
  • All
  • Trace


















Comments

Popular posts from this blog

Microservices Design patterns

What are microservices? Microservices - also known as the microservice architecture - is an architectural style that structures an application as a collection of services that are Highly maintainable and testable Loosely coupled Independently deployable Organized around business capabilities Owned by a small team The microservice architecture enables the rapid, frequent and reliable delivery of large, complex applications. It also enables an organization to evolve its technology stack. You are developing a server-side enterprise application. It must support a variety of different clients including desktop browsers, mobile browsers and native mobile applications. The application might also expose an API for 3rd parties to consume. It might also integrate with other applications via either web services or a message broker. The application handles requests (HTTP requests and messages) by executing business logic; accessing a database; exchanging messages with other systems; and returni...

GraphQL

What is GraphQL  API Standard invented & open-sourced by Facebook Alternative to  REST API  enables declarative data fetching  exposes single endpoint & responds to queries How it works?  Why Graphql? Improvises performance by reducing the data that is to be transferred over the internet Variety of different frontend frameworks and platforms on client-side Fast development speed & expectation for rapid feature development Why Graphql is better than REST? Flexibility & efficient  No more over /under fetching of data Over fetching : Under fetching: Insightful analytics  Schema serves as contract between client and server CORE CONCEPTS : SDL :SCHEMA DEFINITION LANGUAGE Writing Data with mutations 3 kinds of mutations creating new data updating existing data deleting existing data