Skip to content

Commit e9a510c

Browse files
committed
add @async.is_cancellation_error
1 parent f0d9767 commit e9a510c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/async.mbt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ pub using @coroutine {pause}
4848
/// In this case, `is_being_cancelled` can be used to avoid dead loop.
4949
pub using @coroutine {is_being_cancelled}
5050

51+
///|
52+
/// `is_cancellation_error(err)` return `true`
53+
/// if `err` is the special error used to represent cancellation internally.
54+
///
55+
/// Note that `is_cancellation_error` may not be accurate:
56+
/// async code may fail and raise other error during cancellation handling,
57+
/// in this case the error may be replaced by something else.
58+
/// For accurate detection of cancellation, use `is_being_cancelled` instead.
59+
pub fn is_cancellation_error(error : Error) -> Bool {
60+
error is @coroutine.Cancelled
61+
}
62+
5163
///|
5264
pub suberror TimeoutError derive(Show, ToJson)
5365

src/pkg.generated.mbti

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import(
1010
// Values
1111
fn is_being_cancelled() -> Bool
1212

13+
fn is_cancellation_error(Error) -> Bool
14+
1315
fn now() -> Int64
1416

1517
async fn pause() -> Unit

0 commit comments

Comments
 (0)