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

Add functions to convert to / from a Promise to a LazyPromise #12

Merged
merged 3 commits into from
Jun 23, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 7 additions & 1 deletion src/Web/Promise/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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