La rutina matutina para explicar Async/Await

Mi rutina

Rutina matutina

  1. Drink water πŸ₯›

Rutina matutina

  1. Drink water πŸ₯›
  2. Take a shower 🚿
Β 

Rutina matutina

  1. Drink water πŸ₯›
  2. Take a shower 🚿
  3. Get dressed πŸ‘–
Β 
Β 

Rutina matutina

  1. Drink water πŸ₯›
  2. Take a shower 🚿
  3. Get dressed πŸ‘–
  4. Drink juice 🍊🍍
Β 
Β 
Β 

Rutina matutina

  1. Drink water πŸ₯›
  2. Take a shower 🚿
  3. Get dressed πŸ‘–
  4. Drink juice 🍊🍍
  5. Trenzar my hairΒ 
Β 
Β 
Β 
Β 

Rutina matutina

  1. Drink water πŸ₯›
  2. Take a shower 🚿
  3. Get dressed πŸ‘–
  4. Drink juice 🍊🍍
  5. Trenzar my hairΒ 
  6. Drink coffee β˜•οΈ
Β 
Β 
Β 
Β 

A iniciar el trabajo remoto

Β 

3

1. Each one in a row

1. Each one in a row

2. Completing all ASAP no matter the order

πŸ‘– before 🚿?

Create an "Async" function

* StartTime (parameter if we are late for work or not)

const morningRoutine = async (startTime) => {

};

Create an "Async" function

const drinkWater = (time) => new Promise(resolve, reject) => {
  resolve (time);
}

const morningRoutine = async (startTime) => {
  const step1FinishTime = await drinkWater(startTime)
};

Create an "Async" function

const drinkWater = (time) => new Promise(resolve, reject) => {
  resolve (time);
}

const shower = (time) => new Promise(resolve, reject) => {
  resolve (time);
}

const morningRoutine = async (startTime) => {
  const step1FinishTime = await drinkWater(startTime)
  const step2FinishTime = await shower(step1FinishTime)
};

Create an "Async" function

* startTime (parameter if we are late for work or not)

const morningRoutine = async(startTime) => {
   
  Promise 1 resolves
πŸ•’<------------------------πŸ₯›
  |
  |____pass_val
           |_to_promise 2___
                            |  
  Promise 2 resolves        |    
πŸ•’<------------------------🚿

}

Logic

const drinkWater = (time) => new Promise(resolve, reject) => {
  resolve (time);
}

const shower = (time) => new Promise(resolve, reject) => {
  resolve (time);
}

const morningRoutine = async (startTime) => {
  const step1FinishTime = await drinkWater(startTime)
  const step2FinishTime = await shower(step1FinishTime)
  const step3FinishTime = await getDressed(step2FinishTime)
  const step4FinishTime = await drinkJuice(step3FinishTime)
  const step5FinishTime = await trenzarPelo(step4FinishTime)
  const step6FinishTime = await drinkCoffee(step5FinishTime)
  return step6FinishTime;
};

Create an "Async" function

Handling errors

const drinkWater = (time) => new Promise(resolve, reject) => {
  resolve (time);
}

const shower = (time) => new Promise(resolve, reject) => {
  resolve (time);
}

const morningRoutine = async (startTime) => {
try{
  const step1FinishTime = await drinkWater(startTime)
  const step2FinishTime = await shower(step1FinishTime)
  const step3FinishTime = await getDressed(step2FinishTime)
  const step4FinishTime = await drinkJuice(step3FinishTime)
  const step5FinishTime = await trenzarPelo(step4FinishTime)
  const step6FinishTime = await drinkCoffee(step5FinishTime)
  return step6FinishTime;
}
catch (e) {
  return e;
}
};

Using β€œTry” and β€œCatch” In the Async Function

const morningRoutine = async(startTime) => {
  try{
    1πŸ•’<------------------------πŸ₯›
      |_________________________
                                |    
    2πŸ•’<------------------------🚿
      |_________________________
                                |    
    3πŸ•’<------------------------πŸ‘–
      |_________________________
                                |    
    4πŸ•’<------------------------🍍🍊
      |_________________________
                                |    
    5πŸ•’<------------------------🚿
      |_________________________
                                |    
    6πŸ•’<------------------------β˜•οΈ    
  }
  catch (e) {
    return e;
  }

}

Logic

const morningRoutine = async(startTime) => {
  try{
    1πŸ•’<------------------------πŸ₯›
      |_________________________
                                |    
    2πŸ•’<------------------------🚿
      |_________________________
                                |    
    3πŸ•’<------------------------πŸ‘–
      |_________________________
                                |    
    4πŸ•’<------------------------🍍🍊
      |_________________________
                                |    
    5πŸ•’<------------------------🚿
      |_________________________
                                |    
    6πŸ•’<------------------------β˜•οΈ    
  }
  catch (e) {
    return e;
  }

}

Logic

Demo

Gracias

@luucamay_

La rutina matutina para explicar Async/Await

By Lupe Maydana

La rutina matutina para explicar Async/Await

  • 343