Promise Maps

J’aime beau­coup Simon Willi­son depuis des années. Il tient un carnet de notes en guise de blog, comme j’au­rais long­temps voulu avoir le courage de faire.

Il relaie là un commen­taire ycom­bi­na­tor :

When caching the result of an expen­sive compu­ta­tion or a network call, don’t actually cache the result, but cache the promise that awaits the result.

This way, if a new, unca­ched key gets reques­ted twice in rapid succes­sion, ie faster than the compu­ta­tion takes, you avoid compu­ting/fetching the same value twice. […] In other words, the promise acts as a mutex around the compu­ta­tion, and the resul­ting code is unders­tan­dable even by people unfa­mi­liar with mutexes, locks and so on.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.