Skip to content

Infinite recursive promise resolution chains #4

@tzeikob

Description

@tzeikob

Think about adding an anti-pattern about the problem of creating promise chains using infinitely recursive calls.

Below you can find an incomplete draft:

Infinite Promise Chain

The infinite promise chain is the process of chaining promises one after the other in an endless unbounded manner and is close related to critical issues like performance degradation and memory leaks. Promises are offering a very functional way to mitigate such use cases where we need to handle not ending streams of data.

Explanation

Our first thought would be to try a combination of promises and recursion in order to start handling those endless tasks. Let's say we have a trivial use case in which we should print the current date and time in asynchronous manner where each print should be delayed at a given interval. We can implement a delay function which is returning a promise that will be resolve after a given time in seconds.

function delay (secs) {
  return new Promise((resolve, reject) => {
    setTimeout(() => resolve(), secs * 1000); // Resolve after x seconds
  });
}

Now we can use another function called tick which will be responsible to start and maintain the task of printing the current date and time.

Considerations

...

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions