View prototype inheritance.js
function Parent() {
this.prop = "prop";
this.anotherProp = "anotherProp";
}
// can I use arrow functions here?
Parent.prototype.someFunction = function() {
// ...
};
Parent.prototype.anotherFunction = function() {
View generator3.js
function *exponents(start) {
yield start ** 1;
yield start ** 2;
yield start ** 3;
return 4;
}
for (let val of exponents(4)) {
console.log(val);
}
View generator2.js
function *fibonacci(){
let first = 1;
let second = 1;
while (true){
let current = second;
second = first;
first = first + current;
yield current;
}
}
View generator.js
function *someFunction(a) {
const x = yield a*2;
const y = yield x*2;
const z = yield y*2;
console.log(`a:`,a,`x:`,x,` y:`,y,` z:`,z);
// a:5 x:20 y:4 z:8
return x+y+z;
}
View promise4.js
// Multiple sources with the callback pattern
getData((a) => (
getMore(a, b => (
andEvenMore(b, c=> (
andLastlyMore(c, d => (
// do something with d
)
)
)
);
View promise3.js
Promise.All( [somePromise, anotherPromise, andAFinalPromise] ).then( values => {
//values is an array with the resolves from each of the promises.
}).catch( (err) => {
// if one of the promises rejects, the whole fails.
});
Promise.race( [somePromise, anotherPromise, andAFinalPromise] ).then ( value => {
// value is a single value from the promise that resolved or rejected first.
});
View promise2.js
let someFunction = () => {
let combined = ``;
callSomePromise.then( (val) => {
return combined + val;
}
return combined;
}
View promise.js
let myPromise = new Promise( (res, rej) => {
// rather than this function returning, it resolves `res`, or rejects `rej`
if(someCrazyProcess){
res(`it worked`);
} else {
rej(`it didn't work`)
}
});
// then to use the promise
View callback2.js
myAjaxCall({
success: (data) => { /* do something with the data */ },
failure: (err) => { console.error(`something went wrong`, err);
});
View callback.js
const someFunction = (callback) => {
//do some stuff
callback();
}
someFunction( () => {
// something to run that is getting passed into the function I am calling.
});