Skip to main content

Posts

Showing posts from 2020

JavaScript | Type Conversion

  JavaScript | Type Conversion JavaScript is loosely typed language and most of the time operators automatically convert a value to the right type but there are also cases when we need to explicitly do type conversions. While JavaScript provides numerous ways to convert data from one type to another but there are two most common data conversions : Implicit Conversion explicit conversion Implicit Conversion When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type . This is known as implicit conversion.it is call Automatic type conversion Example 1: Implicit Conversion to String      // numeric string used with + gives string type      let sum ;      sum = '3' + 2 ;      console . log ( sum ) // "32"      sum = '3' + true ;      console . log ( sum ); // "3true"      sum = '3' + undefined ;      console . log ( sum ); // "3undefined"      sum = '3'

Promise vs Observable difference

Promise vs Observable difference Promise It resolves or reject a single value and can handle a single value async task at a time. A promise once resolved the async value it completes, can no longer be used.its just one-time use and here it falls short. Not cancellable No rxjs support for operators.  read more about promise Observable ability to emit multiple asynchronous values. Used to handle the stream of events or values. Consider you have an array of numerous tasks or values, and you want every time value is inserted into this it should be handled automatically. Anytime you push a value into this array, all of its subscribers will receive the latest value automatically. Observables are useful for observing input changes, repeated interval, broadcast values to all child components, web socket push notifications

promise

promise  A promise is used to handle the asynchronous result of an operation. JavaScript is designed to not wait for an asynchronous block of code to completely execute before other synchronous parts of the code can run. For instance, when making API requests to servers, we have no idea if these servers are offline or online, or how long it takes to process the server request. in the short term, we can say Promises are used to handle asynchronous Http requests. Promise execution is asynchronous, which means that it's executed, but the program won't wait until it's finished to continue with the rest of the code. Promises have three states: fulfilled : Action related to the promise succeeded rejected : Action related to the promise failed pending : Promise is still pending i.e not fulfilled or rejected yet Creating a Promise The Promise object is created using the  new  keyword and contains the  promise ; this is an executor function which has a  resolve  and a  reject  callb