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