Last active
October 10, 2021 09:23
-
-
Save XoseLluis/956694440475819911a4fd2557c1b315 to your computer and use it in GitHub Desktop.
Use case for coroutines
This file contains 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; | |
} | |
toString(){ | |
return `- ${this.name}: | |
dishes: ${this.dishes? this.dishes.join(", "): ""} | |
desert: ${this.dessert || ""} | |
drinks: ${this.drinks? this.drinks.join(", "): ""} | |
medicines: ${this.medicines? this.medicines.join(", "): ""} | |
`; | |
} | |
} |
This file contains 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 { User } from "./User.js"; | |
import { Meal } from "./Meal.js"; | |
let user = new User("Francoise"); | |
//coroutine that consumes meals, adds medicines to them and sends them to a user | |
function* medicinesAdderFn(user){ | |
while (true){ | |
let medsGroups = [["morning pill"], ["afternoon pill"], ["dinner pill"]]; | |
for (let meds of medsGroups){ | |
let meal = yield null; | |
meal.addMedicines(meds); | |
user.haveMeal(meal); | |
} | |
// let meal = yield null; | |
// meal.addMedicines(["morning pill"]) | |
// user.haveMeal(meal); | |
// meal = yield null; | |
// meal.addMedicines(["afternoon pill"]); | |
// user.haveMeal(meal); | |
// meal = yield null; | |
// meal.addMedicines(["dinner pill"]) | |
// user.haveMeal(meal); | |
} | |
} | |
let meals = [ | |
new Meal("breakfast", ["croissant", "compota"], null, ["coffee", "water"]), | |
new Meal("lunch", ["soup", "beans"], ["flan"], ["water"]), | |
new Meal("dinner", ["pure", "cookies"], null, ["ekko", "water"]), | |
new Meal("breakfast", ["pain au chocolat", "compota"], null, ["coffee", "water"]), | |
new Meal("lunch", ["soup", "pasta"], ["riz au lait"], ["water"]), | |
new Meal("dinner", ["omelet", "magdalenes"], null, ["ekko", "water"]) | |
]; | |
let medicinesAdder = medicinesAdderFn(user); | |
//initial call to "prime" the generator (runs the generator until executing the first yield) | |
medicinesAdder.next(); | |
//normal calls to the generator providing values | |
for (let meal of meals){ | |
medicinesAdder.next(meal); | |
} |
This file contains 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 User{ | |
constructor(name){ | |
this.name = name; | |
} | |
haveMeal(meal){ | |
console.log(`${this.name} is taking meal: | |
${meal.toString()}`); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment