Skip to content

Instantly share code, notes, and snippets.

View anthonyalvarez's full-sized avatar
🏠
Working from home as a remote web coder

Anthony E. Alvarez anthonyalvarez

🏠
Working from home as a remote web coder
View GitHub Profile
@anthonyalvarez
anthonyalvarez / Online-&-Offline-Events.md
Last active November 29, 2018 17:11
Online & Offline Events in JavaScript

Online & Offline Events in JavaScript

By taking advantage of the online and offline events in JavaScript, you are able to notify your users that your web application is no longer online or even perform different actions based on the status.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
@anthonyalvarez
anthonyalvarez / Using-Fetch-to-Load-JSON-file.md
Created October 29, 2018 21:52
Using Fetch to Load a JSON file

Using Fetch to Load a JSON file

const filePath = 'PATH-TO-JSON-DATA-FILE';

var myInit = {  method: 'GET',
                headers: {
                  'Content-Type' : 'application/json'
                },
 mode: 'cors',
@anthonyalvarez
anthonyalvarez / Sending-Form-Data-with-fetch.md
Created October 29, 2018 21:25
Sending AJAX Form Data to the Server with fetch

Sending AJAX Form Data to the Server with fetch

How to use the JavaScript fetch method to send data to the web server. The difference between GET and POST is discussed. How to use FormData to gather and format data to be sent to the web server as part of an AJAX request.

//Sending Data to the Server using Fetch()
//using jsonplaceholder for the data
//GET - queryStrings
// http://jsonplaceholder.typicode.com/posts?userId=1&postId=65
// http://jsonplaceholder.typicode.com/todos?userId=2
@anthonyalvarez
anthonyalvarez / Post-Request-with-Fetch.md
Last active October 29, 2018 21:14
Post Request with Fetch()

Post Request with Fetch()

Fetch is a promise based HTTP request API. It's fairly well supported and is promise based for ease of use and is a perfect fit for those wanting to use out of the box solutions for HTTP.

const dataEndpoint = 'https://jsonplaceholder.typicode.com/posts';

const getPost = () => {
  return fetch(dataEndpoint)
 .then(res =&gt; res.json())
@anthonyalvarez
anthonyalvarez / Failed-HTTP-Request-with-Fetch.md
Last active October 29, 2018 21:41
Handling Failed HTTP Request with Fetch

Handling Failed HTTP Request with Fetch

By default it doesn't produce an error that promises can respond to, so we need to use a different technique.

The fetch() API only rejects a promise when a “network error is encountered, although this usually means permissions issues or similar.” Basically fetch() will only reject a promise if the user is offline, or some unlikely networking error occurs, such a DNS lookup failure.

The good is news is fetch() provides a simple ok flag that indicates whether an HTTP response’s status code is in the successful range or not. For instance the following code logs “Error: Internal Server Error(…)” below.

To keep this code DRY and reusable, you probably want to create a generic error handling function you can use for all of your fetch() calls. The following code refactors the error handling into a handleErrors() function:

@anthonyalvarez
anthonyalvarez / Programmatic-Breakpoint-Control.md
Created October 25, 2018 21:24
Programmatically control breakpoints in Javascript

Programmatically control breakpoints in Javascript

Add a call to a function like __checkDebug(); which will check for a global (or semi-global) variable and when said variable is true, call debugger.

function __checkDebug() {
   if (debugme) debugger;
}
@anthonyalvarez
anthonyalvarez / Microsoft-Surface-Pro-TouchPad-Gestures.md
Last active October 25, 2018 15:39
Microsoft Surface Pro TouchPad Gestures

Microsoft Surface Pro TouchPad Gestures

Single (1) Finger

Single Tap

  • Click Link, button, etc.

  • Select Word

Style Guidelines

The term "Style Guidelines" amd "code conventions" are pften used interchangeably. Style guidelines are a type of code convention aimed at the layout of code within a file. Code conventions can also include programming practices, file directory layout, and commenting.

Having all code look the same is incredibly important on a team, because it allows:

  • Any developer to work on any file regardless of who wrote it. There's no need to spend time reformatting or deciphering the logic of the file, because it looks the same as everything else.

  • Errors becomes more obvious. If all the code looks the same, and you come across some code that does not, you have likely found a problem.

@anthonyalvarez
anthonyalvarez / Maintainable-JavaScript-chap10.md
Last active November 11, 2018 23:38
Throw Your Own Errors

Throw Your Own Errors

Think of errors as built-in failure cases. It is always easier to plan for a failure at a particular point in code than to anticipate failure everywhere. This is common practice in product design, not just code.

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

Throwing Errors in JavaScript

It is more valuable than any other language. You can throw an error by using the throw operator and providing an object to throw. The best way to display a custom error message is to use Error object.

@anthonyalvarez
anthonyalvarez / Maintainable-JavaScript-chap08.md
Last active October 23, 2018 21:05
Avoid Null Comparisons

Avoid Null Comparisons

Comparison to null does not actually prevent errors. Comparing a variable against null typically does not provide enough information about the value to determine whether it's safe to proceed. Fortunately, JavaScript has a number of ways to determine the true calue of a variable.

Detecting Primative Values

There are 5 primitive types in JavaScript:

  1. string
  2. number