1.6.5 • Published 7 years ago

anc-request-all v1.6.5

Weekly downloads
-
License
MIT
Repository
-
Last release
7 years ago

Request All

Request All is a user-friendly JavaScript module for handling large numbers of asynchronous http requests. It uses promises to optimize for readability and ease of use.

Example Usage

const requestAll = require('anc-request-all');

var urls = ['https://www.url1.com', 'https://www.url2.com'] // etc.

requestAll.serial(urls)
	.each(function (body, response) {
		// Execute this function for each valid response
	})
	.catch(function (err) {
		// Execute this function each time a query 
		// responds with an error.
	})
	.finally(function () {
		// Execute this function when all queries are completed.
	})

Parallel vs. Serial Requests

Importing Request All returns an object with two different methods for handling your requests. You can execute your requests in parallel or you can execute your requests serially.

// This returns an object with two methods.
const requestAll = require('anc-request-all');

// It has a parallel method to handle parallel requests
requestAll.parallel(urlArray);

// And it has a serial method to handle serial requests
requestAll.serial(urlArray);

// Both return promises with each, catch, and finally methods.

Parallel Requests

Note: This function mutates the input array.

This function takes in an array of urls and sends requests to each element right away. Invokes your "each" callback for each response, and then executes your "finally" callback when they have all returned. This is much faster than the Serial Request method.

Serial Requests

Note: This function mutates the input array.

This function also take an array of urls but it doesn't request each of them right away. Instead, it pops off the final element in the array and sends a request to it. Once it returns, it recursively pops off the next element and sends another request. This continues until the array is empty. This can be used for very long arrays of urls that cannot be requested in parallel. It also allows for complex control flows because you can add new urls to the url array based on the results of your queries. This is ideal for webscrapers.

Promises

Both methods on the requestAll object return promises with the following methods:

.each

This method accepts a callback function that will execute each time a request comes back. The first argument to the callback function is the request body. The second argument is the request object itself, if you need to access the headers or any other aspect of the request.

.catch

This method accepts a callback funciton that will execute whenever a request comes back with an error. The callback takes the error object as an argument.

.finally

This method accepts a callback function that will execute whenever all requests have been completed. The callback doesn't take any arguments.

1.6.5

7 years ago

1.6.4

7 years ago

1.6.3

7 years ago

1.6.2

7 years ago

1.6.1

7 years ago

1.6.0

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.5

7 years ago

1.2.4

7 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.14

7 years ago

1.1.13

7 years ago

1.1.12

7 years ago

1.1.11

7 years ago

1.1.10

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago