You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Perhaps the best place to put catchAsync would be in questionable, since it seems to be the most useful when combining without/if and await. Normally, using catch with await will catch all CatchableErrors, including CancelledError, which would prevent cancellation propagation, for example:
without collateral =?await sales.context.market.slotCollateral(data.requestId, data.slotIndex).catch:
In this example, slotCollateral return ?UInt256, so we use the binding features of questionable, here, and want to also catch any errors in the process.
This is where something like catchAsync could come into play, which could be used without fear of swallowing CancelledError, for example:
without collateral =?await sales.context.market.slotCollateral(data.requestId, data.slotIndex).catchAsync:
catchAsync could be implemented like this (tested inside of nim-codex):
templatecatchAsync*(body: typed): Result[type(body), refCatchableError] =## Catch exceptions for `body` containing `await` and store them in the## Result, propagating cancellations.#### ```## let r = catchAsync: await someAsyncFuncThatMayRaise()## ```type R =Result[type(body), refCatchableError]
try:
whentype(body) isvoid:
body
R.ok()
else:
R.ok(body)
exceptCancelledErroras eCancelled:
raise eCancelled
exceptCatchableErroras eResultPrivate:
R.err(eResultPrivate)
My personal opinion on this that Future[T, E] is superset of Result[T, E] (it actually could provide all the features of Result[T, E] and add more). Also this is well-known fact that encapsulating big amounts of data into Result[T, E] is extremely ineffective, so even check for an error will cause Nim compiler expand value, so usage of Result[seq[byte], E] is bad idea for performance.
So i do not like idea to have constructions Result[Future[T, E], E] inside nim-chronos codebase, but you can always use this templates in your own codebase.
P.S. sorry i was followed original issue in chronos repo to here and i thought that i responding it in nim-chronos issues.
Perhaps the best place to put
catchAsync
would be in questionable, since it seems to be the most useful when combiningwithout/if
andawait
. Normally, usingcatch
withawait
will catch allCatchableErrors
, includingCancelledError
, which would prevent cancellation propagation, for example:In this example,
slotCollateral
return?UInt256
, so we use the binding features of questionable, here, and want to also catch any errors in the process.This is where something like
catchAsync
could come into play, which could be used without fear of swallowingCancelledError
, for example:catchAsync
could be implemented like this (tested inside of nim-codex):It was determined not to be suited for
nim-results
(arnetheduck/nim-results#54), and as a possibility might eventually be included as a feature set to using Result withnim-chronos
.The text was updated successfully, but these errors were encountered: