https://github.com/articulate/asyncios
https://github.com/articulate/asyncios
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/articulate/asyncios
- Owner: articulate
- License: mit
- Created: 2019-07-19T16:39:50.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-04-10T15:26:00.000Z (over 1 year ago)
- Last Synced: 2025-05-30T05:08:42.023Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 206 KB
- Stars: 1
- Watchers: 56
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# asyncios
A [crocks Async](https://crocks.dev/docs/crocks/Async.html) wrapper for [axios](https://github.com/axios/axios). Allows consumers to take advantage of the "lazy" data type while keeping a familiar API.
## How to Use
`asyncios` accepts the same configuration arguments as the underlying axios function. [See the axios documentation for more details](https://github.com/axios/axios#request-config).
```js
// GET request
asyncios({ method: 'GET', url: '/user?ID=12345' })
.fork(
error => console.log(error),
response => console.log(response)
)
```
```js
// same GET request, but using `params` configuration
asyncios({ method: 'GET', url: '/user', params: { ID: 12345 } })
.fork(
error => console.log(error),
response => console.log(response)
)
```
```js
// POST request
asyncios({
method: 'POST',
url: '/user?ID=12345',
data: { firstName: 'Fred', lastName: 'Flinstone' },
})
.fork(
error => console.log(error),
response => console.log(response)
)
```
### Cancellation
`Async`-style cancellation is supported. See the [crocks doucmentation for more details](https://crocks.dev/docs/crocks/Async.html).
```js
const cancel = asyncios({ method: 'GET', url: '/user?ID=12345' })
.fork(
error => console.log(error),
response => console.log(response),
() => console.log('cancelled!')
)
cancel()
```
`axios`'s cancellation token is also supported. Take note of the different behaviors--while cancelling via crocks will invoke `fork`'s third "cancel" callback, cancelling via axios will invoke `fork`'s first "rejected" callback. [See axios's documentation for more details](https://github.com/axios/axios#cancellation).
```js
const source = axios.CancelToken.source()
asyncios({
method: 'GET',
url: '/user?ID=12345',
cancelToken: source.token
})
.fork(
error => {
if (axios.isCancel(error)) console.log(error.message)
else { /* handle error */ }
},
response => console.log(response)
)
source.cancel('My cancel message')
```
## Thanks
Thanks to the fine people who work on both [axios](https://github.com/axios/axios) & [crocks](https://crocks.dev/).
## The Name
Pretty sure is [spencerfdavis](https://github.com/spencerfdavis)'s fault.