Skip to content

Commit 4cfe94b

Browse files
Updated dependencies;
1 parent 70ed943 commit 4cfe94b

File tree

3 files changed

+10877
-427
lines changed

3 files changed

+10877
-427
lines changed

README.md

+50-13
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,22 @@ This library supports three types of the cancellation API that could be used *s
2828
- [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) signal
2929
- [CPromise](https://www.npmjs.com/package/c-promise2) promise cancellation API
3030

31+
In addition, since cpAxios return a custom promise instead of the native, you get some powers of [CPromise](https://www.npmjs.com/package/c-promise2):
32+
- concurrency limiting fot creating request queues
33+
- progress capturing
34+
- promise timeouts
35+
3136
## Installation :hammer:
3237

38+
Starting from version `0.1.12` the package imports peer dependencies instead of built-in.
39+
So, you should install `c-promise2` and `axios` packages manually using the following command:
40+
3341
```bash
34-
$ npm install cp-axios
42+
$ npm install cp-axios c-promise2 axios
3543
```
3644

3745
```bash
38-
$ yarn add cp-axios
46+
$ yarn add cp-axios c-promise2 axios
3947
```
4048

4149
### CDN bundle
@@ -63,9 +71,7 @@ module global export- `cpAxios`
6371
console.warn(`Request failed: ${err}`)
6472
});
6573

66-
setTimeout(() => {
67-
chain.cancel();
68-
}, 500);
74+
setTimeout(() => chain.cancel(), 500);
6975
````
7076

7177
#### Request aborting using AbortController signal:
@@ -86,9 +92,7 @@ module global export- `cpAxios`
8692
console.warn(`Request failed: ${err}`)
8793
});
8894

89-
setTimeout(() => {
90-
abortController.abort();
91-
}, 500);
95+
setTimeout(() => abortController.abort(), 500);
9296
````
9397

9498
#### Request aborting using Axios cancelToken:
@@ -105,9 +109,7 @@ module global export- `cpAxios`
105109
console.warn(`Request failed: ${err}`)
106110
});
107111

108-
setTimeout(() => {
109-
source.cancel();
110-
}, 500);
112+
setTimeout(() => source.cancel(), 500);
111113
````
112114

113115
#### Using generators as async functions:
@@ -147,13 +149,48 @@ const chain= CPromise.all([
147149

148150
// other request will be aborted if one fails
149151

150-
setTimeout(()=> chain.cancel(), 1000); // abort the request after 1000ms
152+
setTimeout(()=> chain.cancel(), 1000); // abort the requests after 1000ms
153+
````
154+
155+
Making a request queue using mapper function with concurrency limit and progress capturing
156+
[Live Demo](https://codesandbox.io/s/cpromise-all-concurrent-generatorforked-k0fjk?file=/src/index.js):
157+
````javascript
158+
import { CPromise } from "c-promise2";
159+
import cpAxios from "cp-axios";
160+
161+
const promise = CPromise.all(
162+
[
163+
"https://run.mocky.io/v3/7b038025-fc5f-4564-90eb-4373f0721822?mocky-delay=2s&x=1",
164+
"https://run.mocky.io/v3/7b038025-fc5f-4564-90eb-4373f0721822?mocky-delay=2s&x=2",
165+
"https://run.mocky.io/v3/7b038025-fc5f-4564-90eb-4373f0721822?mocky-delay=2s&x=3",
166+
"https://run.mocky.io/v3/7b038025-fc5f-4564-90eb-4373f0721822?mocky-delay=2s&x=4",
167+
"https://run.mocky.io/v3/7b038025-fc5f-4564-90eb-4373f0721822?mocky-delay=2s&x=5",
168+
"https://run.mocky.io/v3/7b038025-fc5f-4564-90eb-4373f0721822?mocky-delay=2s&x=6",
169+
"https://run.mocky.io/v3/7b038025-fc5f-4564-90eb-4373f0721822?mocky-delay=2s&x=7"
170+
],
171+
{
172+
mapper: (url) => {
173+
console.log(`Request [${url}]`);
174+
return cpAxios(url);
175+
},
176+
concurrency: 2
177+
}
178+
)
179+
.innerWeight(7)
180+
.progress((p) => console.log(`Progress: ${(p * 100).toFixed(1)}`))
181+
.then(
182+
(v) => console.log(`Done: `, v),
183+
(e) => console.warn(`Failed: ${e}`)
184+
);
185+
186+
// yeah, we able to cancel the entire task and abort pending network requests
187+
//setTimeout(() => promise.cancel(), 4500);
151188
````
152189

153190
## API Reference
154191

155192
The package exports a wrapped version of the axios instance.
156-
See the axios [documentation](https://www.npmjs.com/package/axios#axios) to gen more information.
193+
See the axios [documentation](https://www.npmjs.com/package/axios#axios) to get more information.
157194

158195
`cpAxios(url, {signal, ...nativeAxiosOptions}): CPromise`
159196

0 commit comments

Comments
 (0)