1
votes

I'm pretty new to purescript and I have a simple purescript demo app that is running on AWS Lambda. I'm trying to get it to talk to S3, which is actually successful, however when the Aff callback finishes executing, the _makeAff javascript function tries to call its internal success callback an additional time. That call fails because success is undefined at that point so then it throws, hits the catch block, and when it tries to call error it throws again and the program terminates.

Here's a simple example to show how I have this set up:

Main.purs

-- I've tried 

myTest "hello world!" >>= (\s -> log $ "from purs " <> s)

-- and

do
  res <- myTest "hello world!"
  log $ "from purs " <> res

Test.purs

module Lib.Test
(myTest)
where

import Control.Monad.Aff (Aff, makeAff, runAff)
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Exception (EXCEPTION, throwException, error, message, try)
import Control.Monad.Aff.Console (log)

import Prelude 

foreign import myTestEff :: forall e . (String -> Eff e Unit) -> String -> Eff e Unit

myTest :: forall e . String -> Aff e String
myTest s = makeAff \reject resolve -> myTestEff resolve s

Test.js

"use strict";

// module Lib.Test

exports.myTestEff = function (cb) {
  return function (s) {
    return function () {
      console.log("from js " , s);
      cb(s)();
    }
  }
};

In the compiled index.js file that pulp makes, the error happens in the _makeAff function:

  exports._makeAff = function (cb) {
    return function(success, error) {
      try {
        return cb(function(e) {
          return function() {
            error(e);
          };
        })(function(v) {
          return function() {
            success(v); // i fail
          };
        })();
      } catch (err) {
        error(err); // then i fail
      }
    }
  }

and this happens AFTER cb(v)(); is complete in Test.js, because I can see the console output from both js and purs in the lambda logs.

Thanks for any assistance.

1

1 Answers

1
votes

From https://github.com/slamdata/purescript-aff/issues/54

... you can't use an Aff value directly as a main, you need to runAff or launchAff it to turn it into an Eff:

main = launchAff do
  a <- later $ pure 42
  b <- later' 1000 $ pure 58
  pure $ a + b