Skip to content

Instantly share code, notes, and snippets.

View whal-e3's full-sized avatar

Eric Whale whal-e3

View GitHub Profile
@whal-e3
whal-e3 / async.js
Created November 15, 2020 04:58
JS async & await
async function myFunc() {
const promise = new Promise((resolve, reject) => {
setTimeout(() => resolve('Hello'), 1000);
});
const err = false;
if (!err) {
const res = await promise; // wait until promise is resolved
return res;
} else {
@whal-e3
whal-e3 / httpLibFetch.js
Created November 14, 2020 12:58
JS custom http library with fetch() & promise
/**
* EasyHTTP Library
* Library for making HTTP requests
*
* @version 2.0.0
* @author Eric Whale
* @license MIT..?
*/
class EasyHTTP {
@whal-e3
whal-e3 / fetch.js
Last active November 14, 2020 10:57
JS fetch() API
// ajax+XHR -> fetch API
document.getElementById('button1').addEventListener('click', getText);
document.getElementById('button2').addEventListener('click', getJson);
document.getElementById('button3').addEventListener('click', getExternal);
// Get local txt data
function getText() {
fetch('test.txt')
.then(res => res.text())
@whal-e3
whal-e3 / promise.js
Created November 14, 2020 10:06
JS es6 Promises
const posts = [
{ title: 'Post one', body: 'this is post one' },
{ title: 'Post two', body: 'this is post two' }
];
function createPost(post) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
posts.push(post);
const error = false;
@whal-e3
whal-e3 / httpLibraryExample.js
Created November 14, 2020 07:32
XHR http library example (GET, POST, PUT DELETE)
function easyHTTP() {
this.http = new XMLHttpRequest();
}
// Make an HTTP GET Request
easyHTTP.prototype.get = function (url, callback) {
this.http.open('GET', url, true);
let self = this;
this.http.onload = function () {
@whal-e3
whal-e3 / asnyc.js
Last active November 13, 2020 09:26
JS Asynchronous - XHR ~ fetch API
// XHR (xmlHTTPrequest) - JSON ---------------------------------------------------------------------
document.getElementById('button2').addEventListener('click', loadCustomers);
function loadCustomers(e) {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'customers.json', true);
xhr.onload = function () {
if (this.status === 200) {
@whal-e3
whal-e3 / es5es6.js
Last active November 9, 2020 10:49
JS es5 vs es6 difference
// Book Constructor
function Book(title, author, isbn) {
this.title = title;
this.author = author;
this.isbn = this.isbn;
}
// UI Constructor
function UI() {}
UI.prototype.addBookToList = function (book) {
@whal-e3
whal-e3 / class.js
Created November 7, 2020 10:49
JS constructor inheritance, Object.create(), class
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.greeting = function () {
return `Hello there ${this.firstName} ${this.lastName}`;
};
const person1 = new Person('John', 'Doe');
@whal-e3
whal-e3 / prototype.js
Last active November 7, 2020 00:53
JS constructor without class and with class - prototype
// Object prototype
// |
// Person prototype
// |
// Person instance ex) john
// Constructor
function Person(firstName, lastName, dob) {
this.firstName = firstName;
this.lastName = lastName;
@whal-e3
whal-e3 / numGuess.js
Created November 4, 2020 14:45
JS Number guessing game - window.location.reload, a === b ? K : P, isNaN(),
/*
GAME FUNCTION:
- Player must guess a number between a min and max
- Player gets a certain amount of guesses
- Notify player of guesses remaining
- Notify the player of the correct answer if loose
- Let player choose to play again
*/
// Game values