Skip to content

Commit

Permalink
Fix return type of tryit (#182)
Browse files Browse the repository at this point in the history
* Fix return type of tryit

The `tryit` utility is super nice but is currently returning `null` in absence
of either an error or a result. This makes it impossible to utilize default
values during destructuring assignment.
  • Loading branch information
hovsater authored Dec 11, 2022
1 parent 5f19016 commit 0d46724
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,9 @@ const sleep = (milliseconds) => {
const tryit = (func) => {
return async (...args) => {
try {
return [null, await func(...args)];
return [void 0, await func(...args)];
} catch (err) {
return [err, null];
return [err, void 0];
}
};
};
Expand Down
4 changes: 2 additions & 2 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ var radash = (function (exports) {
const tryit = (func) => {
return async (...args) => {
try {
return [null, await func(...args)];
return [void 0, await func(...args)];
} catch (err) {
return [err, null];
return [err, void 0];
}
};
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
"engines": {
"node": ">=14.18.0"
}
}
}
6 changes: 3 additions & 3 deletions src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ export const tryit = <TFunction extends (...args: any) => any>(
return async (
...args: ArgumentsType<TFunction>
): Promise<
[Error, null] | [null, UnwrapPromisify<ReturnType<TFunction>>]
[Error, undefined] | [undefined, UnwrapPromisify<ReturnType<TFunction>>]
> => {
try {
return [null, await func(...(args as any))]
return [undefined, await func(...(args as any))]
} catch (err) {
return [err as any, null]
return [err as any, undefined]
}
}
}
8 changes: 4 additions & 4 deletions src/tests/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ describe('async module', () => {
const [err, result] = await _.try(async () => {
throw new Error('not good enough')
})()
assert.isNull(result)
assert.isUndefined(result)
assert.isNotNull(err)
assert.equal(err!.message, 'not good enough')
})
test('returns result when no error is thrown', async () => {
const [err, result] = await _.try(async () => {
return 'hello'
})()
assert.isNull(err)
assert.isUndefined(err)
assert.isNotNull(result)
assert.equal(result, 'hello')
})
Expand Down Expand Up @@ -294,7 +294,7 @@ describe('async module', () => {
return `hi_${num}`
})
})()
assert.isNull(errors)
assert.isUndefined(errors)
assert.deepEqual(results, ['hi_1', 'hi_2', 'hi_3'])
})
test('throws erros as array of all errors', async () => {
Expand All @@ -306,7 +306,7 @@ describe('async module', () => {
})
})()
const err = error as AggregateError
assert.isNull(results)
assert.isUndefined(results)
assert.equal(err.errors.length, 1)
assert.equal(err.errors[0].message, 'number is 2')
})
Expand Down

0 comments on commit 0d46724

Please sign in to comment.