Tuesday, 16 April 2019

Difference between Callbacks and events in NodeJs



The topics "Asynchronous Notifications Using Call-Back Methods" and "Asynchronous Notifications Using Events" demonstrate that call-backs are more work to implement than events. However, you should not base your decision about which to use on the amount of work involved. Call-backs and events represent different approaches to communication, and you should select the approach that best fits your needs.
Events Call backs
An event is like an anonymous broadcast A call-back is like a handshake.
The component that raises events knows nothing about its clients The component that makes call-backs knows a great deal
A client that has a reference to an object that raises events can handle those events . The object that raises the events has no information about its clients. It's broadcasting to an unknown audience — perhaps to an empty theater. By contrast, a component that's making call-backs must have a reference to every object it's going to call. It has to know exactly how many of them there are.
An object that raises events has no control over the order in which clients receive its events. (And you should take care to avoid creating dependencies on any ordering you may observe.) A component making call-backs can control the order in which it calls clients back. It might give some clients higher priority, for example.
When an object raises an event, all of its clients handle the event before the object that raised the event gets control again. a component making call-backs gets control back after each call it makes to a client.
If an event contains a ByRef argument, that argument can be altered by any client that handles the event. The last client's changes are the only ones visible to the object that raised the event, because (as noted above) the object that raised the event doesn't get control again until all of its clients have handled the event. a component making call-backs can examine changes to ByRef arguments after every call to a client, and can pass the next client fresh values for those arguments.
If an unhandled error occurs in a client's event handler, the object that raised the event cannot receive the error. If the object is provided by an in-process component running in the client’s address space, both client and component will terminate as a result of the unhandled error. a component making call-backs will receive errors that occur in the call-back method, and must be prepared to handle them.
Events cannot have optional arguments, named arguments, or ParamArray arguments. Callbacks can have all of these arguments
Example  - Think of a Server (Employee) and Client (Boss).One Employee can have many Bosses. The Employee Raises the event, when he finishes the task, and the Bosses may decide to listen to the Employee event or not.The employee is the publisher and the bosses are subscriber. Example - The Boss specifically asked the employee to do a task and at the end of task done, the Boss wants to be notified. The employee will make sure that when the task is done, he notifies only the Boss that requested, not necessary all the Bosses. The employee will not notify the Boss, if the partial job is done. It will be only after all the task is done.Only one boss requested the info, and employee only posted the reply to one boss.

Tuesday, 19 February 2019

One of My Leads


Lead :
My lead is really good
I wanted to make my lead responsible for a change that I have done by marking my lead as a reviewer but lead wanted to make me independent and confident
tried to help me
Even though he is very busy he helped me in getting my doubts clarified
I am the one who will be creating trouble if I didn't work efficiently but
I will be the reason for the team's bad reputation .. I should never do this
I should respect other's time
I should work as a team
I should listen carefully to other's opinions
I need to listen carefully and fully
I need to be very kind and competent
I need to help others
I need to be confident while solving problems.

Thursday, 10 January 2019

Node JS question and answers

1.      What is Node.js
Node.js is an open source cross platform JavaScript runtime environment. It runs JavaScript outside of the browser. Node is one of the frameworks of JavaScript. Basically, JavaScript was being used client side where scripts written in JavaScript are embedded into HTML pages and executed client side.

node.js enabled developers to write command line tools and applications thus enabling it to work server side. Hence making JavaScript as a unified programming language for application development


2.      Application that receives and processes payments
For payment stripe is used in US. There is node-payments. <I need to study more>


3.       CORS
Cross Origin Resource Sharing

Friday, 21 December 2018

1.     Default parameters:
Before ES6 we had to do these statements to define default parameters:
Text Box: var link = function (height, color, url) {
    var height = height || 50
    var color = color || 'red'
    var url = url || 'http://azat.co'
    ...
}





Text Box: var link = function(height = 50, color = 'red', url = 'http://azat.co') {
  ...
}
They were okay until the value was 0 and because 0 is false in JavaScript it would default to the hard-coded value instead of becoming the value itself. Of course, who needs 0 as a value .so we just ignored this flaw and used the logical OR. we can put the default values right in the signature of the functions:





2.     Template Literals
Template Literals or interpolation is a way to output variables in the string.
Text Box: var name = 'Your name is ' + first + ' ' + last + '.'
var url = 'http://localhost:3000/api/messages/' + id


In ES6, we can use new syntax ${Name} in the back ticked string.
E.g.:
var name = `Your name is ${first} ${last}.`
var url = `http://localhost:3000/api/messages/${id}`

3.     Text Box: var roadPoem = 'Then took the other, as just as fair,\n\t'
    + 'And having perhaps the better claim\n\t'
    + 'Because it was grassy and wanted wear,\n\t'
    + 'Though as for that the passing there\n\t'
    + 'Had worn them really about the same,\n\t'

var fourAgreements = 'You have the right to be you.\n\
    You can only be you when you do your best.'
Multiline Strings
In ES5, to declare a string in multiple lines we had to use one of the below two approaches:
While in ES6, simply utilize the backticks:
var roadPoem = `Then took the other, as just as fair,
    And having perhaps the better claim
    Because it was grassy and wanted wear,
    Though as for that the passing there
    Had worn them really about the same,`

var fourAgreements = `You have the right to be you.
    You can only be you when you do your best.`
4.     De-structuring assignment
var data = $('body').data(), // data has properties house and mouse
  house = data.house,
  mouse = data.mouse
Other examples of destructuring assignments (from Node.js):
var jsonMiddleware = require('body-parser').json
var body = req.body, // body has username and password
  username = body.username,
  password = body.password 
In ES6, we can replace the ES5 code above with these statements:

var {house, mouse} = $('body').data() // we'll get house and mouse variables
var {json: jsonMiddleware} = require('body-parser')
var {username, password} = req.body
This also works with arrays. Crazy!
var [col1, col2]  = $('.column'),
  [line1, line2, line3, , line5] = file.split('\n')
5.     Enhanced Object Literal
In ES5
In ES6
6.     Arrow functions
Using arrows functions in ES6 allows us to stop using that = this or self = this or _this = this or .bind(this). For example, this code in ES5 is ugly:
Text Box: var _this = this
$('.btn').click(function(event){
  _this.sendData()
})





This is the ES6 code without _this = this:
Text Box: $('.btn').click((event) =>{
  this.sendData()
})





7.     Promises
There were a lot of promise implementations with slightly different syntax. q, bluebird, deferred.js, vow, avow, jquery deferred to name just a few. Others said we don’t need promises and can just use async, generators, callbacks, etc. Gladly, there’s a standard Promise implementation in ES6 now!

Let’s consider a rather trivial example of a delayed asynchronous execution with setTimeout():
Text Box: setTimeout(function(){
  console.log('Yay!')
}, 1000)




We can re-write the code in ES6 with Promise:
Text Box: var wait1000 =  new Promise(function(resolve, reject) {
  setTimeout(resolve, 1000)
}).then(function() {
  console.log('Yay!')
})





8.     Block scoped construct let and const
9.     Text Box: class baseModel {
  constructor(options = {}, data = []) { // class constructor
    this.name = 'Base'
    this.url = 'http://azat.co/api'
    this.data = data
    this.options = options
  }
    getName() { // class method
     console.log(`Class name: ${this.name}`)
    }
}
Classes

10.Modules: Native Module support
As you might now, there were no native modules support in JavaScript before ES6. People came up with AMD, RequireJS, CommonJS and other workarounds. Now there are modules with import and export operands.