Skip to content

Commit cb5f7a8

Browse files
committed
Make use of purescript-web-promise
1 parent 3aa74e6 commit cb5f7a8

File tree

4 files changed

+31
-59
lines changed

4 files changed

+31
-59
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
],
1717
"dependencies": {
1818
"purescript-aff": "^7.0.0",
19-
"purescript-foreign": "^7.0.0"
19+
"purescript-foreign": "^7.0.0",
20+
"purescript-web-promise": "purescript-web/purescript-web-promise#^3.1.0"
2021
},
2122
"devDependencies": {
2223
"purescript-assert": "^6.0.0",

src/Control/Promise.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Control/Promise.purs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
1-
module Control.Promise (fromAff, toAff, toAff', toAffE, Promise()) where
1+
module Control.Promise
2+
( module Control.Promise
3+
, module Web.Promise
4+
) where
25

36
import Prelude
47

58
import Control.Alt ((<|>))
69
import Control.Monad.Except (runExcept)
7-
import Data.Either (Either(..), either)
10+
import Data.Either (Either(..), either, hush)
11+
import Data.Maybe (fromMaybe')
812
import Effect (Effect)
913
import Effect.Aff (Aff, makeAff, runAff_)
1014
import Effect.Class (liftEffect)
1115
import Effect.Exception (Error, error)
12-
import Effect.Uncurried (EffectFn1, mkEffectFn1)
13-
import Foreign (Foreign, readString, unsafeReadTagged)
14-
15-
-- | Type of JavaScript Promises (with particular return type)
16-
-- | Effects are not traced in the Promise type, as they form part of the Effect that
17-
-- | results in the promise.
18-
foreign import data Promise :: Type -> Type
19-
20-
type role Promise representational
21-
22-
foreign import promise :: forall a b.
23-
((a -> Effect Unit) -> (b -> Effect Unit) -> Effect Unit) -> Effect (Promise a)
24-
foreign import thenImpl :: forall a b.
25-
Promise a -> (EffectFn1 Foreign b) -> (EffectFn1 a b) -> Effect Unit
16+
import Foreign (readString, unsafeToForeign)
17+
import Web.Promise (Promise, Rejection)
18+
import Web.Promise as Promise
19+
import Web.Promise.Rejection as Rejection
2620

2721
-- | Convert an Aff into a Promise.
28-
fromAff :: forall a. Aff a -> Effect (Promise a)
29-
fromAff aff = promise (\succ err -> runAff_ (either err succ) aff)
22+
fromAff :: forall a. Promise.Flatten a a Aff a -> Effect (Promise a)
23+
fromAff aff = Promise.new (\succ err -> runAff_ (either (err <<< Rejection.fromError) succ) aff)
3024

31-
coerce :: Foreign -> Error
32-
coerce fn =
33-
either (\_ -> error "Promise failed, couldn't extract JS Error or String")
34-
identity
35-
(runExcept ((unsafeReadTagged "Error" fn) <|> (error <$> readString fn)))
25+
coerce :: Rejection -> Error
26+
coerce rej =
27+
fromMaybe'
28+
(\_ → error "Promise failed, couldn't extract JS Error or String")
29+
(Rejection.toError rej <|> map error (hush (runExcept (readString (unsafeToForeign rej)))))
3630

3731
-- | Convert a Promise into an Aff.
3832
-- | When the promise rejects, we attempt to
@@ -44,12 +38,13 @@ toAff = toAff' coerce
4438
-- | Convert a Promise into an Aff with custom Error coercion.
4539
-- | When the promise rejects, we attempt to coerce the error value into an
4640
-- | actual JavaScript Error object using the provided function.
47-
toAff' :: forall a. (Foreign -> Error) -> Promise a -> Aff a
48-
toAff' customCoerce p = makeAff
49-
(\cb -> mempty <$ thenImpl
50-
p
51-
(mkEffectFn1 $ cb <<< Left <<< customCoerce)
52-
(mkEffectFn1 $ cb <<< Right))
41+
toAff' :: forall a. (Rejection -> Error) -> Promise a -> Aff a
42+
toAff' customCoerce p = makeAff \cb →
43+
mempty <$
44+
Promise.thenOrCatch
45+
(\a -> Promise.resolve <$> cb (Right a))
46+
(\e -> Promise.resolve <$> cb (Left (customCoerce e)))
47+
p
5348

5449
-- | Utility to convert an Effect returning a Promise into an Aff (i.e. the inverse of fromAff)
5550
toAffE :: forall a. Effect (Promise a) -> Aff a

test/Main.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module Test.Main where
33
import Prelude
44

55
import Control.Alt ((<|>))
6-
import Control.Monad.Reader.Trans (runReaderT)
7-
import Control.Monad.Reader.Class (class MonadReader, ask, local)
86
import Control.Monad.Except (runExcept)
7+
import Control.Monad.Reader.Class (class MonadReader, ask, local)
8+
import Control.Monad.Reader.Trans (runReaderT)
99
import Control.Promise (Promise)
1010
import Control.Promise as Promise
1111
import Data.Either (either)
@@ -17,7 +17,7 @@ import Effect.Aff.Class (class MonadAff, liftAff)
1717
import Effect.Class (class MonadEffect, liftEffect)
1818
import Effect.Console (log)
1919
import Effect.Exception (error, message)
20-
import Foreign (readString, unsafeFromForeign)
20+
import Foreign (readString, unsafeToForeign)
2121
import Foreign.Index (readProp)
2222
import Test.Assert as Assert
2323

@@ -86,8 +86,8 @@ main = launchAff_ $ flip runReaderT "" do
8686
assert "round-trip result for toAffE is 123" $ res == 123
8787
test "error" do
8888
promise <- liftEffect $ Promise.fromAff $ throwError $ error "err123"
89-
res <- attempt $ Promise.toAff promise
89+
res <- attempt $ Promise.toAff (promise :: Promise Unit)
9090
shouldEqual "err123" $ either message (const "-") res
9191
where
9292
errorCodeCoerce v = either (\_ -> error "fail") error $
93-
(runExcept $ readProp "code" (unsafeFromForeign v) >>= readString)
93+
(runExcept $ readProp "code" (unsafeToForeign v) >>= readString)

0 commit comments

Comments
 (0)