Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Then or catch #16

Merged
merged 2 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Bugfixes:

Other improvements:

## [v3.1.0](https://github.com/purescript-web/purescript-web-promise/releases/tag/v3.0.0) - 2022-07-30

New features:
- Added `thenOrCatch` (#16 by @garyb)

## [v3.0.0](https://github.com/purescript-web/purescript-web-promise/releases/tag/v3.0.0) - 2022-04-27

Breaking changes:
Expand Down
13 changes: 11 additions & 2 deletions src/Web/Promise.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Web.Promise
import Prelude

import Effect (Effect)
import Effect.Uncurried (mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2)
import Effect.Uncurried (mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2, runEffectFn3)
import Web.Promise.Internal (Promise, reject)
import Web.Promise.Internal as P
import Web.Promise.Rejection (Rejection)
Expand All @@ -27,6 +27,15 @@ new k = runEffectFn1 P.new $ mkEffectFn2 \onResolve onReject ->
then_ :: forall a b c. Flatten b c => (a -> Effect (Promise b)) -> Promise a -> Effect (Promise c)
then_ k p = runEffectFn2 P.then_ (mkEffectFn1 k) p

thenOrCatch
:: forall a b c
. Flatten b c
=> (a -> Effect (Promise b))
-> (Rejection -> Effect (Promise b))
-> Promise a
-> Effect (Promise c)
thenOrCatch k c p = runEffectFn3 P.thenOrCatch (mkEffectFn1 k) (mkEffectFn1 c) p

catch :: forall a b. (Rejection -> Effect (Promise b)) -> Promise a -> Effect (Promise b)
catch k p = runEffectFn2 P.catch (mkEffectFn1 k) p

Expand All @@ -40,4 +49,4 @@ all :: forall a. Array (Promise a) -> Effect (Promise (Array a))
all = runEffectFn1 P.all

race :: forall a. Array (Promise a) -> Effect (Promise a)
race = runEffectFn1 P.race
race = runEffectFn1 P.race
4 changes: 4 additions & 0 deletions src/Web/Promise/Internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export function then_(k, p) {
return p.then(k);
}

export function thenOrCatch(k, c, p) {
return p.then(k, c);
}

const catchImpl = function (k, p) {
return p.catch(k);
};
Expand Down
6 changes: 4 additions & 2 deletions src/Web/Promise/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Web.Promise.Internal where
import Prelude

import Effect (Effect)
import Effect.Uncurried (EffectFn1, EffectFn2)
import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3)
import Web.Promise.Rejection (Rejection)

foreign import data Promise :: Type -> Type
Expand All @@ -14,6 +14,8 @@ foreign import new :: forall a b. EffectFn1 (EffectFn2 (EffectFn1 a Unit) (Effec

foreign import then_ :: forall a b c. EffectFn2 (EffectFn1 a (Promise b)) (Promise a) (Promise c)

foreign import thenOrCatch :: forall a b c. EffectFn3 (EffectFn1 a (Promise b)) (EffectFn1 Rejection (Promise b)) (Promise a) (Promise c)

foreign import catch :: forall a b. EffectFn2 (EffectFn1 Rejection (Promise b)) (Promise a) (Promise b)

foreign import finally :: forall a. EffectFn2 (Effect (Promise Unit)) (Promise a) (Promise a)
Expand All @@ -24,4 +26,4 @@ foreign import reject :: forall a. Rejection -> Promise a

foreign import all :: forall a. EffectFn1 (Array (Promise a)) (Promise (Array a))

foreign import race :: forall a. EffectFn1 (Array (Promise a)) (Promise a)
foreign import race :: forall a. EffectFn1 (Array (Promise a)) (Promise a)