mercredi 21 octobre 2015

"Dining Boarders" : Promise with a long-running 'then' function

As a newbie trying to learn the concept of Promises and Futures, I have been amazed by the apparent simplicity of these concepts in asynchronous computations. While trying to write a really long chains of 'then's, I was thinking of this scenario, I am calling it Dining Boarders Situation.

Suppose, we have a promise wakeUpBoarders, after which a function serveBreakfast function should be called. Somehow the breakfasts take a long time to be prepared, so the boarders make the menu choices during supper. When breakfasts are served, the boarders eat them all. Eating meals is a fast operation, making meals is not. When boarders have finished eating, there will be another function named orderForDinner which will take boarders' order, and after a LONG RUNNING TIME, it will resolve a value during dinner.

Now, I could break the orderForDinner into two promises, the first one merely acknowledges having received the order, the second one actually runs in the background. But

  • This second promise must actually succeed. Even if this second callback fails, it should be relaunched so that it is completed with a satisfactory output before dinner.
  • This second phase of execution requires some "business secrets", which should be implemented only in kitchens. So, the orderForDinner should be designed in a way, which returns a quick response to the user, and resumes its long running background task with high guarantee of success.

Which could be a viable way to solve this problem using Promises? A working code demo would be really nice. Does this design has a name, is it a design pattern?

Aucun commentaire:

Enregistrer un commentaire