Skip to content

Commit 377bc1a

Browse files
Added catchErrors option for the useAsyncCallbackHook;
1 parent f2035db commit 377bc1a

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/use-async-effect.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ const argsToPromiseMap = new Map();
158158
* @param {number} [options.queueSize=0]
159159
* @param {boolean} [options.scopeArg= false]
160160
* @param {boolean} [options.states= true]
161+
* @param {boolean} [options.catchErrors= true]
161162
* @returns {UseAsyncCallbackDecoratedFn}
162163
*/
163164
const useAsyncCallback = (generator, options) => {
@@ -180,7 +181,8 @@ const useAsyncCallback = (generator, options) => {
180181
threads,
181182
queueSize = -1,
182183
scopeArg= false,
183-
states= false
184+
states= false,
185+
catchErrors= false
184186
} = options && Array.isArray(options) ? {deps: options} : options || {};
185187

186188
if (threads === undefined) {
@@ -234,7 +236,7 @@ const useAsyncCallback = (generator, options) => {
234236
if (threads || queueSize !== -1) {
235237
let started;
236238

237-
const promise = new CPromise(resolve => {
239+
let promise = new CPromise(resolve => {
238240
const start= ()=> {
239241
current.pending++;
240242

@@ -261,7 +263,7 @@ const useAsyncCallback = (generator, options) => {
261263
start();
262264
}).weight(0)
263265
.then(resolveGenerator)
264-
.finally((value, isRejected) => {
266+
[catchErrors? 'done': 'finally']((value, isRejected) => {
265267
started && current.pending--;
266268
removeElement(promises, promise);
267269
combine && argsToPromiseMap.delete(promise);
@@ -287,7 +289,7 @@ const useAsyncCallback = (generator, options) => {
287289
return promise;
288290
}
289291

290-
const promise = resolveGenerator().finally(() => {
292+
const promise = resolveGenerator()[catchErrors? 'done' : 'finally'](() => {
291293
removeElement(promises, promise);
292294
combine && argsToPromiseMap.delete(promise);
293295
}).weight(0).aggregate();
@@ -296,7 +298,6 @@ const useAsyncCallback = (generator, options) => {
296298

297299
if (combine) {
298300
argsToPromiseMap.set(promise, args);
299-
return CPromise.resolve(promise);
300301
}
301302

302303
return promise;

test/src/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,37 @@ describe("useAsyncCallback", function () {
570570
document.getElementById('root')
571571
);
572572
});
573+
574+
it("should not throw if catchErrors option is set and internal async task was rejected", function (done) {
575+
let counter = 0;
576+
577+
function TestComponent() {
578+
const fn1 = useAsyncCallback(function* () {
579+
throw Error('test');
580+
}, {catchErrors: true});
581+
582+
const fn2 = useAsyncCallback(function* () {
583+
throw Error('test');
584+
}, {catchErrors: false});
585+
586+
Promise.all([
587+
fn1().catch(()=>{
588+
assert.fail('should not throw');
589+
}),
590+
fn2().then(()=>{
591+
assert.fail('should throw')
592+
}, (err)=>{
593+
assert.ok(err instanceof Error);
594+
assert.strictEqual(err.message, 'test');
595+
})
596+
]).then(()=> done(), done);
597+
598+
return <div>Test</div>
599+
}
600+
601+
ReactDOM.render(
602+
<TestComponent/>,
603+
document.getElementById('root')
604+
);
605+
});
573606
});

0 commit comments

Comments
 (0)