Posts

Showing posts from June, 2015

!function in javascript

JavaScript syntax 101. Here is a function declaration: function foo () {} Note that there's no semicolon: this is a function declaration; you need a separate invocation of  foo()  to actually run the function. On the other hand,  !function foo() {}  is an expression, but that still doesn't invoke the function, but we can now use  !function foo() {}()  to do that, as  ()  has higher precedence than  ! . Presumably the original example function doesn't need a self-reference so that the name then can be dropped. So what the author is doing is saving a byte per function expression; a more readable way of writing it would be this: ( function (){})();

Understanding JavaScript Promises

via: https://spring.io/understanding/javascript-promises Understanding JavaScript Promises A promise represents the eventual result of an asynchronous operation. It is a placeholder into which the successful result value or reason for failure will materialize. Why Use Promises? Promises provide a simpler alternative for executing, composing, and managing asynchronous operations when compared to traditional callback-based approaches. They also allow you to handle asynchronous errors using approaches that are similar to synchronous  try/catch . Promise States A promise can be in one of 3 states: Pending - the promise’s outcome hasn’t yet been determined, because the asynchronous operation that will produce its result hasn’t completed yet. Fulfilled - the asynchronous operation has completed, and the promise has a value. Rejected - the asynchronous operation failed, and the promise will never be fulfilled. In the rejected state, a promise has a  reason  that indicates wh