r/purescript • • Jun 09 '18

How to call asynchronous functions from `createLifecycleComponent`'s eval function ?

I am using the library: https://github.com/doolse/purescript-reactnative
And I have been trying for hours to make a call to a JS function that returns a Promise from within an action evaluator.
My code looks like this:

1- Linking.js:

const RN = require('react-native'); exports.getInitialURLImpl = function(v) {    return RN.Linking.getInitialURL(); } 

2- Linking.purs:

foreign import data LINKING :: Effect foreign import getInitialURLImpl :: forall eff. Unit -> Eff eff (Promise String)  getInitialURL :: forall eff. Aff (linking :: LINKING | eff) String getInitialURL = liftEff (getInitialURLImpl unit) >>= toAff 

3- Main.purs:

initialState = {} data Action = HandleDeepLinking app :: forall p. ReactClass p app = createLifecycleComponent (didMount $ HandleDeepLinking) initialState render2 eval   where     eval (HandleDeepLinking) = void $ launchAff do         x <- getInitialURL         liftEff $ alert "test" (Just x)         pure unit 

The above app
value does not typecheck. I don't know how to be able to access the result of a Promise from within the action evaluator.
Help!!!

1 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/attilah Jun 11 '18

I've been able to fix this issue. I unfortunately didn't include an additional function in my FFI:

I should have written this: exports.getInitialURLImpl = function(v) { return function() { return RN.Linking.getInitialURL(); } }

Instead of this: exports.getInitialURLImpl = function(v) { return RN.Linking.getInitialURL(); }