@widergy/polling-handling
Version:
Utility GO! Polling Handling
82 lines (55 loc) • 2.21 kB
Markdown
# Polling Handling 


[](https://github.com/semantic-release/semantic-release)
```
< Hello! can you give me the user data?
> Hi, I don't have that information, but my friend 83abb91c-31f0-4be6-9b8b-88cbf716c can get it for you.
< Mmm... ok.. Hey 83abb91c-31f0-4be6-9b8b-88cbf716c can you give me the user data?
> Processing
< 🤔 Ok ... now?
> Processing
< 🤔 Ok ... now?
> Processing
< 😒 now?
> OK! { userInfo: ... }
< Great! 😁
```
## Installing
```
npm install --save @widergy/polling-handling
yarn add @widergy/polling-handling
```
## Basic usage
#### On application starts
```js
import Polling from '@widergy/polling-handling';
class App extends Component {
componentDidMount = () => {
Polling.init('Polling endpoint base url, for example: http://apibaseurl.com/api/v1/async_request/jobs/');
....
```
#### When you need to consume a service with polling
```js
import Polling from '@widergy/polling-handling';
// Service that triggers async worker generation
const someService = parameters => api.get('/some_endpoint', parameters);
....
// When we need to consume that service, and poll the async worker to obtain info:
const response = await Polling.handle(someService(parameters));
```
## Interface
### Input
#### Polling.init(baseUrl)
- baseUrl (Required): Endpoint base for all async jobs, then handle concats job id to this url.
#### Polling.handle(originalRequest, timeout)
- originalRequest (Required): Api call that returns job code for poll.
- timeout (Optional): For override the default timeout value (60000 milliseconds).
### Output
If requests fails returns a response object with two keys:
- error: Complete error details from api answer.
- status: Status code from api answer.
If request finish ok, a response object with only one key:
- data: Complete information from api answer.
## License
MIT © [Widergy](https://www.widergy.com/)