polling-request Travis Circle npm package

A simple way to query async APIs.

Installation

Using npm:

$ npm install --save polling-request

Usage

Lets asume we have the following API endpoint:

request: POST /generateReport
response: {jobId: 1, status: 'RUNNING'}

It can take between 30 seconds and 10 minutes for the back end to generate a report.

Luckily for us there is another endpoint to check the job status:

request: GET /job/{jobId}
response: {status: 'RUNNING|FINISHED', report: { }}

polling-request takes care of polling and lets us focus on writing business logic:

import request from 'polling-request'
const expectedJSON = { status: 'FINISHED' }

let promise = request('/jobs/1', expectedJSON)

// polling after 1min: { status: 'RUNNING' }
// polling after 2min: { status: 'RUNNING' }
// polling after 3min: { status: 'FINISHED' }
promise.then(response => {
  showReport(response.report)
})

TODOs

  • add optional parameter that defines failing JSON. Ex: { status: 'ERROR' }. This allows polling-request to fail fast without waiting for the timeout

License

MIT