Chained Promise: functional programming tools for recurring promises
We often find recurring patterns when handling asynchronous logic with promises, such as an HTTP endpoint that paginates and gives you a URL pointer to the next available dataset.
Chained Promise provides an extended Promise class that you can use to easily abstract out recurring patterns. See jsdocs for more detailed explanations.
Example
Suppose we are querying Wikipedia API to get the list of all linked pages from “Plato” page:
The data field contains the material content of the value, while the next field contains either the promise to the next batch of data, or {[ChainedPromise.DONE]: lastNode} which ChainedPromise recognizes to be the terminal node.
Now that the chaining of the value has been configured, we can work on the series of data.
This executes the given callback function, and the result itself is a promise that resolves into the value of the terminal node when it reaches the end.
Chained Promise: functional programming tools for recurring promises
We often find recurring patterns when handling asynchronous logic with promises, such as an HTTP endpoint that paginates and gives you a URL pointer to the next available dataset.
Chained Promiseprovides an extended Promise class that you can use to easily abstract out recurring patterns. See jsdocs for more detailed explanations.Example
Suppose we are querying Wikipedia API to get the list of all linked pages from “Plato” page:
With
fetchwe can turn the end point into a promise. Then we can useChainedPromiseto extend the promise:First thing we want to do is to parse the resulting JSON:
Now we have a promise that resolves into a JS object. Next we need to map the result into the format that
ChainedPromiseis expecting.The
datafield contains the material content of the value, while thenextfield contains either the promise to the next batch of data, or{[ChainedPromise.DONE]: lastNode}whichChainedPromiserecognizes to be the terminal node.Now that the chaining of the value has been configured, we can work on the series of data.
This executes the given callback function, and the result itself is a promise that resolves into the value of the terminal node when it reaches the end.
See the example project for the full example code. Also see jsdoc to ChainedPromise.ts for more explanation of other functions such as
flatMap.Disclaimer: This is not an official Google product.