Skip to content

Instantly share code, notes, and snippets.

@fdnklg
Last active August 20, 2019 10:58
Show Gist options
  • Save fdnklg/9b998393dc103f1efabf2da9538a06a0 to your computer and use it in GitHub Desktop.
Save fdnklg/9b998393dc103f1efabf2da9538a06a0 to your computer and use it in GitHub Desktop.

How to request your rest API in a paginated way with parallel promises

  1. In your API backend count all documents included in your mongodb and create a handler function for it. A detailed view about the API code can be found here.
db.collection.find({ you db query here }).count()
  1. In the frontend split your requests in smaller bits of requests and while following this design pattern:
  • Create a wrapper function that reaturns a promise with the counted documents.
fetchAllDocuments() {
    let promiseArr = [];
    let offset = 0;
    let iterator

    return new Promise((resolve, reject) => {
            this.fetched = [];

            this.collectCountTrees()
            .then(data => {
                iterator = Math.ceil(data / this.limit);

                for (let index = 0; index < iterator; index++) {
                    const offset = index == 0 ? 0 : index * this.limit;
                    promiseArr.push(this.collectTrees(offset, this.limit))
                }

                Promise.all(promiseArr).then(data => {
                    this.fetched = this.fetched.flat();
                }).catch(err => {
                    console.log(err)
                });

            }).catch(err => {
                console.log(err)
            });
    });
}
  • Create another function that build the url for your api request. This funciton returns another function which returns another promise. This one handles your api request and fetches the data
collectData(skip, limit) {
    const arr = [this.state.min, this.state.max];
    const query = `?start=${arr[0]}&end=${arr[1]}&skip=${skip}&limit=${limit}`;
    const path = "http://yourApiUrlHere";
    const url = `${path}${query}`;

    return this.promiseGet(url);
}

promiseGet(url) {
    return new Promise((resolve, reject) => {
        axios.get(url)
            .then(res => {
                if (res.data.length > 1) {
                    this.fetched.push(res.data);
                }
                resolve(res.data);
            })
            .catch(err => {
                console.log(err);
                reject(err);
        })
    })
}
  • Create an array with all promises that are necessary to fetch all documents included in your statement (like in the example of the first function block)

  • Use Promise.all([promiseArr]).then(data => {}) to run all requests in parallel and resolve when all requests are done (see in the first function block)

  • flatten your responses into one array with array.flat()

This pattern will increase your performance when requesting large amounts of data from a database via an API..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment