This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Meal{ | |
constructor(name, dishes, dessert, drinks){ | |
this.name = name | |
this.dishes = dishes; | |
this.dessert = dessert; | |
this.drinks = drinks; | |
} | |
addMedicines(medicines){ | |
this.medicines = medicines; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const smallValue = 100; | |
const mediumValue = 1000; | |
const largeValue = 50000; | |
function factorial(n) { | |
//return (n !== 0) ? n * factorial(n - 1) : 1; | |
if(n === 0 || n === 1){ | |
return 1; | |
} | |
else{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let st = "helloworld"; | |
//normal recursive (non tail recursion) function | |
function recursiveTransformString (source){ | |
if (source.length === 0) | |
return ""; | |
if (source.length === 1) | |
return source.toUpperCase() + source; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { from as asyncIterableFrom } from 'ix/asynciterable/index.js'; | |
import { filter as asyncFilter, map as asyncMap } from 'ix/asynciterable/operators/index.js'; | |
function asyncSleep(interval){ | |
return new Promise(resolveFn => setTimeout(resolveFn, interval)); | |
} | |
async function findCountryForCityAsync(city){ | |
let countries = [ | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//sort of inner join | |
//improved version where we directly obtain the alias from the parameter name in the selector functions | |
function joinCollectionsImproved(collection1, collection2, idSelectorFn1, idSelectorFn2){ | |
let getAlias = selectorFn => selectorFn.toString().split("=>")[0].trim(); | |
let [alias1, alias2] = [idSelectorFn1, idSelectorFn2].map(getAlias); | |
let result = []; | |
collection1.forEach(it1 => { | |
let id1 = idSelectorFn1(it1); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//returns a new Promise expanded with inspection methods | |
function enableSyncInspect(pr){ | |
//we trap these variables in the closure making them sort of private, and allow public access only through the inspection methods that we add to the new promise | |
let isFulfilled = false; | |
let value = null; //resolution result | |
let isRejected = false; | |
let reason = null; //rejection reason | |
let isPending = true; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Implement Synchronous Inspection like the one provided by bluebirdjs | |
//http://bluebirdjs.com/docs/api/synchronous-inspection.html | |
class SyncInspectPromise extends Promise{ | |
constructor(executorFn){ | |
//compiler forces me to do a super call | |
super(() => {}); | |
this._isFulfilled = false; | |
this._value = null; //resolution result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function composeMethods(...methods){ | |
//use a normal function, not an arrow, as we need "dynamic this" | |
return function(...args) { | |
methods.forEach(method => { | |
let methodArgs = args.splice(0, method.length); | |
method.call(this, ...methodArgs); | |
}) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//wrap setTimeout with an async-await friendly function | |
function sleep(ms){ | |
console.log("sleep starts"); | |
let resolveFn; | |
let pr = new Promise(res => resolveFn = res); | |
setTimeout(() => { | |
console.log("sleep ends"); | |
resolveFn(); | |
}, ms); | |
return pr; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//helper function to use in the async function containing multiple async calls | |
function checkCancelation(cancelationToken){ | |
if (cancelationToken && cancelationToken.reject){ | |
console.log("throwing"); | |
throw new Error("Rejection forced"); | |
} | |
if (cancelationToken && cancelationToken.cancel){ | |
console.log("cancelling"); | |
//return a Promise that never resolves | |
return new Promise(()=>{}); |