ES8 – Callback, Promise and Async

Callbacks: Pass in a function as a parameter

function fetchData(callback) { 
    callback(); 
}

 

Promise: States: Pending, Fulfilled, Rejected

function fetchData() { 
    return new Promise((resolve, reject) => { 
        resolve(rides), 

        // if error:
        reject(errorInfo)
    });
};

 

Promise.all:
takes multiple promises and returns a promise which resolves when all supplied promises are done.

Promise.race:
takes multiple promises and resolves when the first promise is done.

 

async()


// Running sequentially: asyncFunc1 is run before asyncFunc2
async () => { 
    await asyncFunc1(); 
    await asyncFunc2(); 
}


// Running in parallel
async () => { 
    await Promise.all([asyncFunc1(), asyncFunc2()]); 
}

 

ES8 – Array and Exponent

ES8 aka. ES2017

Array.prototype custom function (review)

let number = [1, 2, 3, 4]; 
let moreNumbers = new Array (1, 2, 3, 4); 

Array.protype.count = function() { 
    return this.length;
}

console.log(numbers.count());     // outputs 4
console.log(moreNumbers.count()); // outputs 4

 

Array.prototype.includes

Check if an array contains a specific value and returns a boolean.

let letters = ['a', 'b', 'c', NaN]; 
letters.includes('b');      // true
letters.includes('d');      // false

// includes is similar to indexOf from older version 
letters.indexOf('b') >= 0   // true

// when includes and indexOf is different: 
letters.includes(NaN)       // true
letters.indexOf(NaN) >= 0   // false

 

Exponential syntax: **

3 ** 4 = Math.pow(3, 4)

 

Bash: Variable – filename example

Variable in bash is not particularly cool until we have a good use of it  😛

> filename="myfile.txt"
> touch $filename
> ls filename
myfile.txt

> rm $filename
> ls $filename
ls: myfile.txt: No such file or directory

> files="file1 file2" 
> touch $files
> ls $files
file1
file2

 

Bash: `type` Command

In unix, type command tells us whether a keyword is taken by the system or not. It is useful when trying to add a new custom command in the terminal and ensure that our command is not in conflict with system ones.

> type test
test is a shell builtin

> type cp
cp is /bin/cp

> type foo
-bash: type: foo: not found

# This is my custom command
> type note  
note is hashed (/Users/.../bin/note)

 

5 Mindsets for Living to the Fullest

This post is a note from a video on 5 Hard Lessons You Need to Learn Before 30. I’m not sure why the magic number 30 is there since everyone’s age span varies a bit differently, but anyways, here are the five lessons:

1. There may not be a tomorrow, so enjoy/do it now.

  • Think about what would be the things that you would regret not do if tomorrow were your last day.
  • Your time is limited and you don’t have forever, so do what you want to and do what you enjoy right now.

2. You’ve got nothing to lose, but everything to gain.

  • Give it your all. You are born to win, so do what winners do

Continue reading