Understanding Promises in JavaScript : asynchronous behaviour of Javascript Part -2

In the previous article, we learned about using callback for performing asynchronous operations. In ES06/ES2015, Javascript introduced something called Promises which helps you handle  asynchronous operations in a better way The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises provide a better way for managing asynchronous operations when compared to traditional callback-based way. Promises represent data that may not be available but will be available later upon completing some asynchronous operation. A promise can be called a placeholder for future data and  has 3 states PENDING RESOLVED REJECTED Immediately on creation, the state of a Promise is pending. Resolved state means the asynchronous operation is fulfilled. Rejected means that the asynchronous operation is not completed and has some errors. Many Javascript native apis for asynchronous operations support promises since ES06. Prior to that many libraries including JQuery and Bluebird were used to create promises for doing asynchronous operations. Example for promises JavaScript fetch method returns a promise.

You can see that immediately the state of promise is pending. Now for resolving the promise and getting the output Promise.prototype.then() and Promise.prototype.catch() Promise.prototype.then() and Promise.prototype.catch() The promise object has a then method that returns the promise…

Prototypes and Prototypal inheritance in Javascript

The word prototype means an early model or a primary version of a machine or a product.All further version created from that prototype will inherit the properties of that prototype.Prototype-based programming is a style of object-oriented programming in which behaviour reuse is performed via a process of reusing existing objects via delegation that serve as prototypes.(Wikipedia). Nearly all objects in JavaScript are instances of Object which sits on the top of a prototype chain.The Object.prototype is on the top of the prototype inheritance chain. That means almost all the objects in JavaScript inherit the properties and methods from Object.prototype.JavaScript Date objects inherit from Date.prototype. Array objects inherit from Array.prototype. Prototype determines the behaviour of a data object.Properties including methods are inherited from the prototype.See the below array which inherits prototypes from Array.prototype.You can see is array,map,reduce etc which are inherited from Array.prototype Prototypal inheritance In Javascript a function is a first-class Object.All JavaScript objects inherit properties and methods from a prototype.

In the example above, function Person() is called object constructor function.Wth the new keyword,we can create new objects fusing the constructor function.

both myGo and myGoGo has properties first,last,age,eyecolor and  prototype We cannot add a new property to an Person directly.we can add it to its prototype.

Share this page in social media platforms