Skip to content

Instantly share code, notes, and snippets.

View prof3ssorSt3v3's full-sized avatar
🎯
Focusing

Steve Griffith prof3ssorSt3v3

🎯
Focusing
View GitHub Profile
//String split and Array join methods
//converting Strings to Arrays and Arrays to Strings
//String to Array str.split();
//Array to String arr.join();
//
let sentence = "Hello my name is Inigo Montoya";
let words = sentence.split(" ").sort();
console.log(words);
// Function Currying
//In JavaScript, functions are first-class objects,
// just like String, Number, Boolean
//This means that they can be passed to functions or returned from functions
//
function greet(msg){
//console.log(msg);
return function(name){
console.log(msg, name);
// setTimeout
// How to use setTimeout, clearTimeout, setInterval
// and clearInterval
//
function log(msg){
console.log('MESSAGE', msg);
clearInterval(tommy);
}
// Callback functions
//built-in callback functions
//setTimeout, Arr.forEach, geolocation.getCurrentPosition
//make your own callback functions
//setTimeout( hello, 2000, 'Bob')
let names = ['Inga','Tom','Mattias','Carlos'];
names.forEach(hello);
// Default Function Parameters
// new ES6 feature allowing us to provide
//default values for function parameters
function sendMessage(email, message, title="message"){
if(!email || !message){
return false;
}
//title = title || "";
//fancy code to send the message
// ES6 Destructuring
//extracting values from objects and arrays
//and assigning them to multiple variables
let name, id, nm, num, star, planet, rest;
let personObj = {name:"Arthur Dent", id:42, planet:'Earth'};
let personArr = ["Zaphod", 123, 'Betelgeuse'];
//[nm, num] = personArr;
//console.log(nm, num);
//Promises - Just the Basic Facts
//wrappers for anything async
// ajax calls, reading files, timeouts, geolocation, talk to a database, or anything that would use a callback function
//use them to get rid of callback hell
//fetch() returns a Promise.
//var result = multiplyTwoNumbers(5, 10);
//console.log(result); //50
//
//var photo = downloadPhoto('http://localhost/cat.jpg');
// ES6 Promises - More syntax and examples
// calling resolve directly
// calling reject directly
//
let p1 = new Promise((resolve, reject)=>{
if(true){
resolve('p1 resolved');
}else{
reject('p1 rejected');
// Promise.all()
//when you only want to run your code after ALL
// your promises are resolved.
// EG: fetching remote data from multiple locations
let p1 = () => Promise.resolve('Got the list of users');
let p2 = () => Promise.resolve('Got the list of tweets');
let p3 = Promise.resolve('Got the weather');
Promise.all([p1(), p2(), p3]).then((resultsArr)=>{
// Promise.race()
// when you only want the result from the first
// resolved promise
//
let p1 = Promise.reject(111);
let p2 = Promise.resolve(222);
let p3 = new Promise((resolve, reject)=>{