Skip to content

Commit 662dfdc

Browse files
Expose cancel method for useAsyncEffect hook;
1 parent 87251af commit 662dfdc

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

lib/use-async-effect.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ const removeElement = (arr, element) => {
2727
/**
2828
* @typedef {function} UseAsyncEffectCancelFn
2929
* @param {string} [reason]
30+
* @property {Boolean} done
31+
* @property {*} result
32+
* @property {*} error
33+
* @property {boolean} canceled
34+
* @property {UseAsyncEffectCancelFn} cancel
3035
* @property {PauseFn} pause
3136
* @property {ResumeFn} resume
3237
* returns {boolean}
@@ -46,15 +51,15 @@ const removeElement = (arr, element) => {
4651

4752
/**
4853
* AsyncEffect hook to define cancellable effects
49-
* @param {GeneratorFunction} generator
54+
* @param {GeneratorFunction} generatorFn
5055
* @param {object} [options]
5156
* @param {deps} [options.deps= []]
5257
* @param {boolean} [options.skipFirst= false]
5358
* @param {boolean} [options.states= false]
5459
* @param {boolean} [options.once= false]
5560
* @returns {UseAsyncEffectCancelFn}
5661
*/
57-
const useAsyncEffect = (generator, options) => {
62+
const useAsyncEffect = (generatorFn, options) => {
5863
let {current} = useRef({});
5964

6065
let {
@@ -74,7 +79,7 @@ const useAsyncEffect = (generator, options) => {
7479

7580
const [state, setState] = states ? useAsyncDeepState(initialState, {watch: false}) : [];
7681

77-
if (!isGeneratorFn(generator)) {
82+
if (!isGeneratorFn(generatorFn)) {
7883
throw TypeError('useAsyncEffect requires a generator as the first argument');
7984
}
8085

@@ -120,7 +125,7 @@ const useAsyncEffect = (generator, options) => {
120125

121126
states && setState(initialState);
122127

123-
let promise = current.promise = CPromise.run(generator, {resolveSignatures: true, scopeArg: true})
128+
let promise = current.promise = CPromise.run(generatorFn, {resolveSignatures: true, scopeArg: true})
124129
.then(result => {
125130

126131
current.done = true;
@@ -186,6 +191,7 @@ const useAsyncEffect = (generator, options) => {
186191
cancel.error = state.error;
187192
cancel.canceled = state.canceled;
188193
cancel.paused = state.paused;
194+
cancel.cancel= cancel;
189195
}
190196

191197
return cancel;
@@ -270,7 +276,7 @@ const asyncEffectFactory= (options) => {
270276

271277
/**
272278
* useAsyncCallback hook for defining cancellable async routines
273-
* @param {GeneratorFunction} generator
279+
* @param {GeneratorFunction} generatorFn
274280
* @param {object|array} [options]
275281
* @param {array} [options.deps= []]
276282
* @param {boolean} [options.combine= false]
@@ -282,7 +288,7 @@ const asyncEffectFactory= (options) => {
282288
* @param {boolean} [options.catchErrors= true]
283289
* @returns {UseAsyncCallbackDecoratedFn}
284290
*/
285-
const useAsyncCallback = (generator, options) => {
291+
const useAsyncCallback = (generatorFn, options) => {
286292
const current = useFactory(asyncEffectFactory, [options]);
287293

288294
let {
@@ -327,7 +333,7 @@ const useAsyncCallback = (generator, options) => {
327333
}
328334
}
329335

330-
const resolveGenerator = () => CPromise.run(generator, {args, resolveSignatures: true, scopeArg});
336+
const resolveGenerator = () => CPromise.run(generatorFn, {args, resolveSignatures: true, scopeArg});
331337

332338
cancelPrevious && !combine && cancel(E_REASON_RESTART);
333339

0 commit comments

Comments
 (0)