11module Angular.Promise
22 ( Promise ()
3+ , PromiseEC ()
34 , then1
45 , then1'
56 , then2
@@ -8,18 +9,28 @@ module Angular.Promise
89 , then3'
910 , catch
1011 , finally
12+ , liftPromiseEC
13+ , runPromiseEC
1114 ) where
1215
16+ import Control.Monad.Cont.Trans (ContT (..), runContT )
1317import Control.Monad.Eff
18+ import Control.Monad.Error (Error )
19+ import Control.Monad.Error.Trans (ErrorT (..), runErrorT )
20+ import Control.Monad.Trans (lift )
21+
22+ import Data.Either (Either (Right, Left))
1423import Data.Function
1524
1625foreign import data Promise :: * -> * -> *
1726
27+ type PromiseEC e a b = ErrorT a (ContT Unit (Eff e )) b
28+
1829foreign import then1Fn
1930 " " "
20- function then1Fn(f, fa){
21- return fa['then'](f);
22- }
31+ function then1Fn(f, fa){
32+ return fa['then'](f);
33+ }
2334 " " " :: forall a b c d . Fn2 (b -> c ) (Promise a b ) (Promise a d )
2435
2536then1 :: forall a b c . (b -> Promise a c ) -> Promise a b -> Promise a c
@@ -30,9 +41,9 @@ then1' = runFn2 then1Fn
3041
3142foreign import then2Fn
3243 " " "
33- function then2Fn(f, g, fa){
34- return fa['then'](f, g);
35- }
44+ function then2Fn(f, g, fa){
45+ return fa['then'](f, g);
46+ }
3647 " " " :: forall s t a b c d . Fn3 (b -> d ) (a -> c ) (Promise a b ) (Promise s t )
3748
3849then2 :: forall a b c d . (b -> Promise c d ) -> (a -> Promise c d ) -> Promise a b -> Promise c d
@@ -43,11 +54,11 @@ then2' = runFn3 then2Fn
4354
4455foreign import then3Fn
4556 " " "
46- function then3Fn(f, g, h, fa){
47- return fa['then'](f, g, function(a){
48- return h(a)();
49- });
50- }
57+ function then3Fn(f, g, h, fa){
58+ return fa['then'](f, g, function(a){
59+ return h(a)();
60+ });
61+ }
5162 " " " :: forall e q r s t a b c d . Fn4 (b -> d )
5263 (a -> c )
5364 (s -> Eff e t )
@@ -62,20 +73,45 @@ then3' = runFn4 then3Fn
6273
6374foreign import catchFn
6475 " " "
65- function catchFn(f, fa){ \
66- return fa['catch'](f); \
67- }
76+ function catchFn(f, fa){
77+ return fa['catch'](f);
78+ }
6879 " " " :: forall a b c d . Fn2 (a -> Promise c d ) (Promise a b ) (Promise c d )
6980
7081catch :: forall a b c d . (a -> Promise c d ) -> Promise a b -> Promise c d
7182catch = runFn2 catchFn
7283
7384foreign import finallyFn
7485 " " "
75- function finallyFn(f, fa){
76- return fa['finally'](f);
77- }
86+ function finallyFn(f, fa){
87+ return fa['finally'](f);
88+ }
7889 " " " :: forall e r a b . Fn2 (Eff e r ) (Promise a b ) (Promise a b )
7990
8091finally :: forall e r a b . Eff e r -> Promise a b -> Promise a b
8192finally = runFn2 finallyFn
93+
94+ foreign import then2ECFn
95+ " " "
96+ function then2ECFn(f, g, fa){
97+ return function(){
98+ var run = function(k){
99+ return function(a){
100+ return k(a)();
101+ };
102+ };
103+ fa['then'](run(f), run(g));
104+ };
105+ }
106+ " " " :: forall e a b c d . Fn3 (b -> d ) (a -> c ) (Promise a b ) (Eff e Unit )
107+
108+ promiseEC :: forall e a b . Promise a b -> PromiseEC e a b
109+ promiseEC = ErrorT <<< ContT <<< cb
110+ where
111+ cb fa k = runFn3 then2ECFn (k <<< Right ) (k <<< Left ) fa
112+
113+ liftPromiseEC :: forall e a b . (Error a ) => Eff e (Promise a b ) -> PromiseEC e a b
114+ liftPromiseEC fa = (lift $ lift fa) >>= promiseEC
115+
116+ runPromiseEC :: forall e a b . PromiseEC e a b -> (Either a b -> Eff e Unit ) -> Eff e Unit
117+ runPromiseEC = runContT <<< runErrorT
0 commit comments