Skip to content

Instantly share code, notes, and snippets.

@BriceShatzer
Last active January 2, 2018 17:37
Show Gist options
  • Save BriceShatzer/a1bc9156a7a13e233da71536dba3884a to your computer and use it in GitHub Desktop.
Save BriceShatzer/a1bc9156a7a13e233da71536dba3884a to your computer and use it in GitHub Desktop.
//basic for loop
for (var i = 0; i < Things.length; i++) {
Things[i]
if(condition){
return; //breaks out
}
}
//Array.forEach version
let things = [1,2,3];
things.forEach(function(element, index, array){
console.log(element);
//1, 2, 3
});
//for..of - loop over iterables (Array,Map,Set,String,TypedArray,arguments object)
let iterable = [10, 20, 30];
for (let value of iterable) {
value += 1;
console.log(value);
// 11, 21, 31
}
//for..in - loop over enumerable properties of an object
let obj = {a: 1, b: 2, c: 3};
for (var prop in obj) {
console.log(prop);
//1, 2, 3
}
//fetching
fetch(url, fetchOptions)
.then(function(fetchResponse){
if(fetchResponse.ok){
return fetchResponse.json();
} else {
console.log(url + " failed");
displaySearchErrorMessage(fetchResponse.statusText);
}
})
//IFFE
(function(){
//--- code ---
}())
//basic API fetch function
function apiCall(url) {
return fetch(url)
.then(function (fetchResponse) {
if(fetchResponse.ok){
return fetchResponse.json();
} else { throw error}
})
.then(function (json){
//return json.data;
return json;
})
.catch(function (error) {
console.log(error.message);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment