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

Commit e0e9dee

Browse files
Add functions to convert to / from a Promise to a LazyPromise (#12)
1 parent 485ddf0 commit e0e9dee

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Added `toPromise` and `fromPromise` helper functions to the `LazyPromise` module for convenient transformations between the `Promise` and `LazyPromise` types (#12 by @thomashoneyman)
1011

1112
Bugfixes:
1213

src/Web/Promise/Lazy.purs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Data.Traversable (traverse)
77
import Effect (Effect)
88
import Effect.Class (class MonadEffect)
99
import Effect.Uncurried (mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2)
10-
import Web.Promise (Executor, Rejection)
10+
import Web.Promise (class Flatten, Executor, Rejection)
1111
import Web.Promise.Internal as P
1212

1313
-- | A trivial box that adds a layer between promises to prevent automatic flattening.
@@ -68,3 +68,9 @@ race :: forall a. Array (LazyPromise a) -> LazyPromise a
6868
race as = LazyPromise do
6969
as' <- traverse (\(LazyPromise a) -> a) as
7070
runEffectFn1 P.race as'
71+
72+
fromPromise :: forall a. Effect (P.Promise a) -> LazyPromise a
73+
fromPromise p = LazyPromise $ runEffectFn2 P.then_ (mkEffectFn1 (pure <<< P.resolve <<< Box)) =<< p
74+
75+
toPromise :: forall a b. Flatten a b => LazyPromise a -> Effect (P.Promise b)
76+
toPromise (LazyPromise p) = runEffectFn2 P.then_ (mkEffectFn1 \(Box b) -> pure (P.resolve b)) =<< p

0 commit comments

Comments
 (0)