From 19a959e20a5762b9ba2174577b804f62ed2708c7 Mon Sep 17 00:00:00 2001 From: Thomas Honeyman Date: Mon, 21 Jun 2021 14:52:39 -0700 Subject: [PATCH 1/3] Add to / from lazy promise functions --- src/Web/Promise/Lazy.purs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Web/Promise/Lazy.purs b/src/Web/Promise/Lazy.purs index 5933e6d..a4c724b 100644 --- a/src/Web/Promise/Lazy.purs +++ b/src/Web/Promise/Lazy.purs @@ -68,3 +68,12 @@ race :: forall a. Array (LazyPromise a) -> LazyPromise a race as = LazyPromise do as' <- traverse (\(LazyPromise a) -> a) as runEffectFn1 P.race as' + +fromPromise :: forall a. Effect (P.Promise a) -> LazyPromise a +fromPromise p = LazyPromise $ runEffectFn2 P.then_ (mkEffectFn1 (pure <<< P.resolve <<< Box)) =<< p + +toPromise :: forall a. LazyPromise a -> Effect (P.Promise a) +toPromise (LazyPromise p) = unbox =<< p + where + unbox :: forall b. P.Promise (Box b) -> Effect (P.Promise b) + unbox = runEffectFn2 P.then_ (mkEffectFn1 \(Box b) -> pure (P.resolve b)) From adc6a981360b80b40dacfb578a6edd2a0eed85aa Mon Sep 17 00:00:00 2001 From: Thomas Honeyman Date: Mon, 21 Jun 2021 14:58:08 -0700 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63f9c2e..b75c328 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based Breaking changes: New features: +- Added `toPromise` and `fromPromise` helper functions to the `LazyPromise` module for convenient transformations between the `Promise` and `LazyPromise` types (#12 by @thomashoneyman) Bugfixes: From 9360ea330b2449c3263a297e241f60af3189d15c Mon Sep 17 00:00:00 2001 From: Thomas Honeyman Date: Tue, 22 Jun 2021 09:39:38 -0700 Subject: [PATCH 3/3] Add Flatten constraint --- src/Web/Promise/Lazy.purs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Web/Promise/Lazy.purs b/src/Web/Promise/Lazy.purs index a4c724b..04afe4a 100644 --- a/src/Web/Promise/Lazy.purs +++ b/src/Web/Promise/Lazy.purs @@ -7,7 +7,7 @@ import Data.Traversable (traverse) import Effect (Effect) import Effect.Class (class MonadEffect) import Effect.Uncurried (mkEffectFn1, mkEffectFn2, runEffectFn1, runEffectFn2) -import Web.Promise (Executor, Rejection) +import Web.Promise (class Flatten, Executor, Rejection) import Web.Promise.Internal as P -- | A trivial box that adds a layer between promises to prevent automatic flattening. @@ -72,8 +72,5 @@ race as = LazyPromise do fromPromise :: forall a. Effect (P.Promise a) -> LazyPromise a fromPromise p = LazyPromise $ runEffectFn2 P.then_ (mkEffectFn1 (pure <<< P.resolve <<< Box)) =<< p -toPromise :: forall a. LazyPromise a -> Effect (P.Promise a) -toPromise (LazyPromise p) = unbox =<< p - where - unbox :: forall b. P.Promise (Box b) -> Effect (P.Promise b) - unbox = runEffectFn2 P.then_ (mkEffectFn1 \(Box b) -> pure (P.resolve b)) +toPromise :: forall a b. Flatten a b => LazyPromise a -> Effect (P.Promise b) +toPromise (LazyPromise p) = runEffectFn2 P.then_ (mkEffectFn1 \(Box b) -> pure (P.resolve b)) =<< p