var calculatedNumber = Math.pow(2,4);
calculatedNumber;
ES2016
var calculatedNumber = 2**4;
calculatedNumber;
ES2015
var nums = [1,2,3,4];
var total = 2;
for(let i = 0; i < nums.length; i++){
total = Math.pow(total, nums[i]);
}
ES2016
var nums = [1,2,3,4];
var total = 2;
for(let i = 0; i < nums.length; i++){
total **= nums[i];
}
var nums = [1,2,3,4,5];
nums.indexOf(3) > -1;
nums.indexOf(44) > -1;
ES2016
var nums = [1,2,3,4,5];
nums.includes(3);
nums.includes(44);
"awesome".padStart(10);
"awesome".padStart(10,'!');
padEnd
"awesome".padEnd(10,'!');
async
async function first(){
return "We did it!";
}
first(); // returns a promise
first().then(val => console.log(val));
async function getMovieData(){
console.log("starting!");
var movieData = await $.getJSON('https://omdbapi.com?t=titanic&apikey=thewdb');
// this line does NOT run until the promise is resolved!
console.log("all done!");
console.log(movieData);
}
getMovieData() // logs an object with data about the movie!
// No .then or callback or yield necessary!
Object Async
var movieCollector = {
data: "titanic",
async getMovie(){
var response = await $.getJSON(`https://omdbapi.com?t=${this.data}&apikey=thewdb`);
console.log(response);
}
}
movieCollector.getMovie();
Class Async (ES2015)
class MovieData {
constructor(name){
this.name = name;
}
async getMovie(){
var response = await $.getJSON(`https://omdbapi.com?t=${this.name}&apikey=thewdb`);
console.log(response);
}
}
var m = new MovieData('shrek');
m.getMovie();
Using try
and catch
block
async function getUser(user){
try {
var response = await $.getJSON(`https://api.github.com/users/${user}`);
console.log(response.name);
} catch(e){
console.log("User does not exist!");
}
}
getUser('elie'); // Elie Schoppik
getUser('foo'); // User does not exist!
SEQUENTIAL NOT PARALLEL
async function getMovieData(){
var responseOne = await $.getJSON(`https://omdbapi.com?t=titanic&apikey=thewdb`);
var responseTwo = await $.getJSON(`https://omdbapi.com?t=shrek&apikey=thewdb`);
console.log(responseOne);
console.log(responseTwo);
}
getMovieData();
MUCH FASTER!
async function getMovieData(){
var titanicPromise = $.getJSON(`https://omdbapi.com?t=titanic&apikey=thewdb`);
var shrekPromise = $.getJSON(`https://omdbapi.com?t=shrek&apikey=thewdb`);
var titanicData = await titanicPromise;
var shrekData = await shrekPromise;
console.log(titanicData);
console.log(shrekData);
}
getMovieData();
SAME but use Promise.all
async function getMovieData(first, second){
var moviesList = await Promise.all([
$.getJSON('https://omdbapi.com?t=${first}&apikey=thewdb'),
$.getJSON('https://omdbapi.com?t=${second}&apikey=thewdb')
]);
console.log(moviesList[0].Year);
console.log(moviesList[1].Year);
}
getMovieData('shrek', 'blade');
// 2001
// 1998
Exercise: Async Functions (pending)
1) Write a function called hasMostFollowers, which accepts a variable number of arguments. You should then make an AJAX call to the Github User API (https://developer.github.com/v3/users/#get-a-single-user) to get the name and number of followers of each argument. The function should return a string which displays the username who has the most followers.
Hint - Try to use Promise.all to solve this and remember that the jQuery AJAX methods ($.getJSON, $.ajax, etc.) return a promise.
hasMostFollowers('elie','tigarcia','colt').then(function(data){
console.log(data)
});
//output: "Colt has the most followers with 424"*/
2) Write a function called starWarsString, which accepts a number. You should then make an AJAX call to the Star Wars API (https://swapi.co/ ) to search for a specific character by the number passed to the function. Your function should return a promise that when resolved will console.log the name of the character.
starWarsString(1).then(function(data){
console.log(data)
})
// output: "Luke Skywalker" */
Bonus 1 - Using the data from the previous AJAX call above, make another AJAX request to get the first film that character is featured in and return a promise that when resolved will console.log the name of the character and the film they are featured in
starWarsString(1).then(function(data){
console.log(data)
})
// output: "Luke Skywalker is featured in The Empire Strikes Back,
//directed by Irvin Kershner"
Bonus 2 - Using the data from Bonus 1 - make another AJAX call to get the information about the first planet that the film contains. Your function should return a promise that when resolved will console.log the name of the character and the film they are featured in and the name of the planet.
starWarsString(1).then(function(data){
console.log(data)
})
// Output: "Luke Skywalker is featured in The Empire Strikes Back,
// directed by Irvin Kershner and it takes place on Hoth"
var instructor = {first:"Elie", last:"Schoppik", job:"Instructor", numSiblings:3};
var { first, last, ...data } = instructor
first;
last;
data;
var instructor = {first:"Elie", last:"Schoppik", job:"Instructor"};
var instructor2 = {...instructor, first:"Tim", last:"Garcia"}; //replace first and last
instructor2;
var defaults = {job: "Instructor", ownsCat:true, ownsDog: true};
defaults;
var matt = {...defaults, ownsCat: false};
matt;
var colt = {...defaults, ownsDog: false};
colt;