Async Await Chaining

Async Await Chaining

Creating synchronously chained functions is straight forward. Codepen.

const UserCollectionSync = {  
  find(id=''){
    document.writeln('1');
    return this;
  },
  update(){
    document.writeln('2');
    return this;
  },
  save(){
    document.writeln('3');
    return this;
  }
}
UserCollectionSync  
  .find()
  .update()
  .save();

But when we decide to just throw some async await flags on those functions everything doesn't work. FOLLOWING CODE IS WRONG: Codepen.

// delay is used to represent some asynchronous work
function delay(ms){  
  return new Promise(resolve => setTimeout(resolve, ms));
} //end delay()

const UserCollectionAsync = {  
  async find(id=''){
    await delay(1000)
    document.writeln('1');
    return this;
  },
  async update(){
    await delay(50);
    document.writeln('2');
    return

Read More

Cleaner complex IF Statements

Cleaner complex IF Statements

So you have this long if statement that's relatively simple in design but ugly to the eyes and somewhat hard to read.

if(x1>=img.left&&x1<=img.left+img.current.width&&  
   y1>=img.top&&y1<=img.top+img.current.height&&
   x2>=img.left&&x2<=img.left+img.current.width&&
   y2>=img.top&&y2<=img.top+img.current.height){
  img.scaling = Math.pow(x1-x2,2)+Math.pow(y1-y2,2);
} //end if

What can we do to make it

Read More