@@ -28,14 +28,22 @@ This library supports three types of the cancellation API that could be used *s
28
28
- [ AbortController] ( https://developer.mozilla.org/en-US/docs/Web/API/AbortController ) signal
29
29
- [ CPromise] ( https://www.npmjs.com/package/c-promise2 ) promise cancellation API
30
30
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
+
31
36
## Installation :hammer :
32
37
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
+
33
41
``` bash
34
- $ npm install cp-axios
42
+ $ npm install cp-axios c-promise2 axios
35
43
```
36
44
37
45
``` bash
38
- $ yarn add cp-axios
46
+ $ yarn add cp-axios c-promise2 axios
39
47
```
40
48
41
49
### CDN bundle
@@ -63,9 +71,7 @@ module global export- `cpAxios`
63
71
console .warn (` Request failed: ${ err} ` )
64
72
});
65
73
66
- setTimeout (() => {
67
- chain .cancel ();
68
- }, 500 );
74
+ setTimeout (() => chain .cancel (), 500 );
69
75
````
70
76
71
77
#### Request aborting using AbortController signal:
@@ -86,9 +92,7 @@ module global export- `cpAxios`
86
92
console .warn (` Request failed: ${ err} ` )
87
93
});
88
94
89
- setTimeout (() => {
90
- abortController .abort ();
91
- }, 500 );
95
+ setTimeout (() => abortController .abort (), 500 );
92
96
````
93
97
94
98
#### Request aborting using Axios cancelToken:
@@ -105,9 +109,7 @@ module global export- `cpAxios`
105
109
console .warn (` Request failed: ${ err} ` )
106
110
});
107
111
108
- setTimeout (() => {
109
- source .cancel ();
110
- }, 500 );
112
+ setTimeout (() => source .cancel (), 500 );
111
113
````
112
114
113
115
#### Using generators as async functions:
@@ -147,13 +149,48 @@ const chain= CPromise.all([
147
149
148
150
// other request will be aborted if one fails
149
151
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);
151
188
````
152
189
153
190
## API Reference
154
191
155
192
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.
157
194
158
195
` cpAxios(url, {signal, ...nativeAxiosOptions}): CPromise `
159
196
0 commit comments