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: diff --git a/src/Web/Promise/Lazy.purs b/src/Web/Promise/Lazy.purs index 5933e6d..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. @@ -68,3 +68,9 @@ 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 b. Flatten a b => LazyPromise a -> Effect (P.Promise b) +toPromise (LazyPromise p) = runEffectFn2 P.then_ (mkEffectFn1 \(Box b) -> pure (P.resolve b)) =<< p