Async/await in JavaScript : asynchronous behaviour of Javascript Part -4

For part 1, part 2 and part 3 of this series, see the following links https://truetocode.com/understanding-callbacks-and-callback-hell-in-javascript-asynchronous-behavior-of-javascript-part-1/697/ https://truetocode.com/understanding-promises-in-javascript-asynchronous-behavior-of-javascript-part-2/730/ https://truetocode.com/generator-functions-in-javascript-asynchronous-behavior-of-javascript-part-3/744/ In the previous three articles in this series, we learned about doing asynchronous operations with callbacks, promises, and generator functions. In ES8 or ES2017 , Javascript introduced a new kind of syntax called async/await, which makes it easier to do asynchronous operations. Async/await syntax is actually just syntactic sugar over promises and callbacks The following example shows the basic syntax for performing async/await operations. Here too, we use fetch function which returns a promise. Remember that this block of code is run on chrome 60. Earlier versions of Chrome do not support async/ await. fetch()  and  json() returns promise

Now using async/await

Both will produce output like  the following The fetch() here returns a promise so we await for fetch. The json() function also returns a promise so we await for that too. Here we can see that using the async/await made the code easier to read and understand. As you can see that async/await helps you to write code in a way that asynchronous code looks and behave a  more like synchronous code. On each await keyword, the function pauses as you can see in the below…

How to accurately check date range to prevent overlapping:mysql
Snippets , Requests / 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

 

“use strict” – Understanding Strict Mode in JavaScript
interview questions , Javascript / June 10, 2018

While using certain JavaScript libraries,you might have seen some thing like

  inside the code.Many of us doesn’t exactly know what this exactly mean.In this article we take a look at the JavaScript  strict mode(ECMAScript 5) which might become handy while  appearing in JavaScript Developer Interviews. Strict mode literal expression, ignored by earlier versions of JavaScript,  lets us create secure JavaScript code.It  enforces better programming practices and removes some of the language’s insecure and ill-advised features.Strict mode throws exception to some of the basic coding bloopers and throws error to some insecure actions that are usually ignored in normal mode. Use strict can be used on top of the file or can be used inside a function in the following way 1  In a file

2 Specifically inside a function

Things that are prevented in strict  1  It prevents use of variables with out declaration.

2 Deleting function or variable is not allowed in strict mode  

3 Prevent global  access

4 Duplicating object properties not allowed

5 Cannot duplicate parameters of a function

 

6 eval is not allowed to create variable

7  Writing…

Implementing priority queue in PHP
PHP , programming concepts / June 2, 2018

Queue is a First In First Out(FIFO) Data Structure.In a priority queue, an element with high priority is served before an element with low priority.The Standard PHP Library has the SplPriorityQueue class which can be used to implement priority queue.The insert(‘item’,priority) method is used to insert item and the corresponding priority in to the queue.See the below example to see mail sending to different mail id’s that have different priorities.You can use while to iterate thorough the queue and process each one or the results. SplPriorityQueue A priority queue can be implemented using many of the data structures that we’ve already studied (array, inked list, binary search tree). However, those data structures do not provide the most efficient operations. To make all of the operations  efficient, heap can be used . SplPriorityQueue is implemented using Max Heap

     

Array map functions in Javascript
Javascript / June 1, 2018

Conside a Javascrpt Array of numbers.We need to make an array which contains squares of all these numbers.What we can do to achieve this is using a for loop.

For doing these Javascript has another function called MAP function on array.Using the map function the above result can be achieved in the following way

Array Map on Array of objects in Javascript

 

js/nodejs/angularjs/ts interview questions
interview questions / May 31, 2018

1  What is data binding in angularjs Data binding in AngularJS is the synchronization between the model and the view.                                      2  What are the key features of angularjs    Modules.    Directives.    Expressions.    Controller.    Scope.    Data Binding.    Validations.    Filters.    Services.  Routing.  Dependency Injection.  Testing. 3  What are directives AngularJS extends HTML attributes with directives 4  What are controllers Controllers are functions which provide data and logic to the HTML page we are going to visualize the data. 5  What do you mean by compile and link in angularjs To have a complete understanding of angularjs understanding the angular is lifecycle is necessary .Angularjs lifecycle        has three phases. Bootstrap phase- The bootstrap phase is the phase in which the library is loaded,module is created based on your ng-app and any                dependencies to your module is loaded Compilation phase- The  compilation phase is the phase which include two steps Traversing the  DOM and collecting all the directives and        then linking the directives to the…

Share this page in social media platforms