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

In the previous article we learned about using callback for processing asynchronous requests.In ES06 Javascript introduced something called Promises which helps you deal with 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 has 3 states PENDING RESOLVED REJECTED Immediately that state of a Promise is pending.Resolved state means the asynchronous operation is fulfilled.Rejected means that the asynchronous operation was not completed and have some errors. Many Javascript native apis for asynchronous operations support promises since ES06 .Prior to that many libraries including JQuery provided us promises for doing asynchronous operations. Example for promises Javscript fetch operation 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 which returns the promise and a catch method which catches the error.See the following set of examples,first on promise…

How to accurately check date range to prevent overlapping:mysql
Requests , Snippets / July 29, 2018

Q:I have a table with events , and each event has an auditorium id,start_date and end_date.In need to insert a new event in to the table without any overlapping on date range in an auditorium   select IF(count(*)>0, true, false) from events where ‘YOUR_START_DATE‘ <= end_date and ‘YOUR_END_DATE‘ >= start_date; OR select IF(count(*)>0, true, false) from events where ‘YOUR_START_DATE‘  BETWEEN start_date AND end_date or ‘YOUR_END_DATE‘ BETWEEN start_date and end_date and end_date or (‘YOUR_START_DATE‘ < start_date and ‘YOUR_END_DATE‘ > end_date); If true do not proceed with insert if false continue

 

Creating a Cron job in Laravel 5.2 :Task Scheduling
Beginner , PHP , Snippets / July 23, 2018

To create a cron job in Laravel 5.2 ,move to the app/console/ folder.The console folder contains all the relevant configuration files and folders for you to create a custom artisan command in Laravel 5.6 .  After creating the custom artisan command ,we can schedule the command to be run in specific intervals Step 1 Run

  to create new custom artisan command,This will generate a class called app/Console/Commands/CronJob.php.The newly generated file will have the following structure.You have to fill in name and description variables in the file.You can use the DB and MODEL files from CronJob.php. The file extends the Command Class   app/Console/Commands/CronJob.php

Here we select a set of users based on some custom criteria from the users table and do something to them periodically with the cron job.The action for the should be written in the CronJob.php – handle() function.The class defines our command.To run this need to use the following php artisan cronjob Step 2 Scheduling the job To schedule the when to run the command we have created open the kernel.php file in the app/console/ folder app/console/kernel.php

The kernel class extends ConsoleKernel .To add the commands

and to schedule the command

  This runs…

Understanding Callbacks and Callback hell in JavaScript : asynchronous behaviour of Javascript Part -1

In  most programming languages , functions can accept other function(s) as arguments or return a function .Such functions are called Higher order functions. In JavaScript most APIs and Modules can accept function as an argument to a function .The functions are called  callbacks. Structure of callbacks

Note that JavaScript functions behave as objects.We can set a function as an object property or store a function into a variable. JavaScript is a non-blocking programming language.It doesn’t wait or block  an operation to finish but keeps on  executing.For example consider the case when data is fetched asynchronously,then javascript doesn’t wait for the asynchronous call to get data but  keeps executing the next line of code.So in order to do something with the data we can use callbacks.See the below example in which we use a setimeout to wait for the data just like an XHR request which may take some time to complete

Asynchronous callback with  Jquery and a callback function

  CALLBACK HELL Consider the above example in which we use a callback function which is passed as an argument to the $.get function in Jquery. Now what happens when we want to send subsequent requests is that the code becomes unreadable…

Sending email with SendGrid and Node.js – Using HTML templates:Step by step tutorial
Javascript and Node.js , Snippets / July 15, 2018

SendGrid is a communication platform that helps you manage communications with your clients via e-mail.SendGrid helps you manage email sending,design templates using code editor/design editor,run marketing campaign etc.To use send grid with your Node.js application,you need to integrate @sendgrid/mail package to your application.Follow the steps below to integrate and configure mail sending in Node.js with SendGrid   STEP 1 Create a SendGrid account using your email,provide the necessary information and confirm your account by clicking the confirmation mail from sendgrid   STEP 2 Sign in to your SendGrid account and go to dashboard.In the left side settings you will find an option called Api keys. Click on Api keys and generate a new api key for your sendgrid account.   STEP 3 – Integrating #sendgrid/mail To install sendgrid/mail for node run the following in the terminal.

In your node application,use the following code to import @sendgrid/mail

  STEP – 3 Creating a template in sendgrid To create aSendGrid template, go to the template section in the left side of your dashboard.Create a template  and then click the action button. From the action button choose the option New Version which will give you the following screen with design editor and code editor.Choose…

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.

Difference between a Framework and a Library : Javascript point of view

While attending interviews for programming jobs,one of the most common question you would face is to explain the difference between a libarary and a framework.Here in this article we look at the difference between a library and a framework from a Javascript point of view. Library Libraries provide methods/functions/modules  that can be useful while building your application.While using a library the control is with your code and you call the functions the libraries provide from within  your code.These functions are reusable and can be used at different parts of building your application.There are many Javascript libraries like JQuery,JqueryUI,moment.js etc.These libraries provides functions that serves different purposes.   So we can generally describe the working of a library as We call the library functions from from our code Flow Control is with our code                For example when using JQuery library, we include the library in our code and call the library functions from within our code in the following way

Framework A framework provides structure,resources for building your application.While using a framework you call your logic from within the framework .The control is with the framework code , your code is called when  logic is…

How to display user – display name,avatar and logout button in WordPress : Example
Beginner , PHP , Snippets , Wordpress / July 8, 2018

To display user displayname , avatar and provide a logout button for a user in your wordpress site,we can use the following  five worpress functions. is_user_logged_in () –  Check whether used in. wp_get_current_user(id/email,null)  –  Get current logged in user get_avatar_url(userid) – Get avatar url for current user using userid wp_logout_url(url) – Logout and redirect URL home_url() – Get your home URL  

  Check below links to see documentation in full https://developer.wordpress.org/reference/functions/is_user_logged_in/ https://developer.wordpress.org/reference/functions/wp_get_current_user/ https://developer.wordpress.org/reference/functions/get_user_by/ https://developer.wordpress.org/reference/functions/wp_logout_url/        

first-child and nth-child selectors : Styling multiple Elements with same class – CSS
Beginner , css , Uncategorized / July 7, 2018

first-child  – This selector selects first child of its parent. To color first one of elements with the same class,we use first-child selector.Consider below example with multiple divs(.box)  inside parent div(.container).To change the style of first div

  nth-child – This selector selects nth child of its parent. We can achieve the above result using nth-child selector .The number in the bracket indicates the order of the child

 

How to change ‘sql_mode’ in mysql
PHP , Uncategorized / July 2, 2018

To see Mysql mode settings,do the following query. Mysql mode is not linked to any particular database. Modes can be set locally to the session (connection), or for the server globally.

This gives results like below   Now to change sql_mode,you need to run query like this

Example Some times after  importing database and changing some table structure,you may get an error like this. This is because NO_ZERO_IN_DATE and NO_ZERO_DATE is enabled in and database has invalid date values. NO_ZERO_IN_DATE The NO_ZERO_IN_DATE mode affects whether the server permits dates in which the year part is nonzero but the month or day part is 0. Example – ‘2010-00-01’ or ‘2010-01-00’ NO_ZERO_DATE This mode is deprecated This determines whether the server permits ‘0000-00-00’ . NO_ZERO_IN_DATE also depends on whether strict SQL mode is enabled. Removing above modes from sql_mode  lets the server allow invalid dates in the aforementioned format     Or to set it PERMANANTLY, change etc/my.cnf

and restart WHAT is sql_mode Server SQL modes determine what SQL syntax MySQL should support and what kind of data validation checks it should use. This makes it easier to use MySQL in different environments. See more on  list of sql…

Share this page in social media platforms