Promises are a simple abstraction to handle deferred and asynchronous computations.
This lunchtime I decided to see how one would go about implementing two concepts that are present in the Promise standard (
This function is used to compose multiple supplied promises into a single returned promise that provides the resulting values in array form. Looking at the implementation documented below you can see how relativity easy it is to compose these promises together, adding logic in-between resolutions to accumulate the final value returned.
We are able to use this solution in a couple of contrived examples, initially highlighting the ‘happy path’ of three separate promises being resolved, before moving on to handling the case were a promise is rejected.
Following on from the
all implementation we are able to implement the
race function that is also present in the standard.
This function returns the first complete (resolved or rejected) promise from a supplied collection.
Wrapping the supplied promises within a new promise who’s resolved and rejected function are called based on their actions, allows you to see how the race condition works.
We are able to highlight this implementation in an example, first depicting the ‘happy path’, followed by a rejection which is returned due to ‘winning the race’.