1

I have a function that does some logic then runs a success callback function or a fail callback function.

Here is the code:

some_function() ->
    ArgumentsForCallback = [],
    check_some_condition(Input, fun success_callback/1, fun fail_callback/1).

How do I pass ArgumentsForCallback to the callbacks?

2
  • Can you please post the source of check_some_condition? Does it call the callback functions with 0 arguments? Commented Aug 5, 2016 at 8:44
  • You are right, callback functions are called without arguments which seems to be a wrong way. Should I pass arguments to check_some_condition and then call callback functions with them from inside check_some_condition function? Commented Aug 5, 2016 at 8:48

1 Answer 1

7

There are two ways:

  1. Pass the argument to check_some_condition and make that function send the argument to the callbacks:

    check_some_condition(Input, ArgumentsForCallback, Success, Fail) ->
        Success(ArgumentsForCallback).
    
    some_function() ->
        ArgumentsForCallback = [],
        check_some_condition(Input, ArgumentsForCallback, fun success_callback/1, fun fail_callback/1).
    
  2. Send anonymous functions to check_some_condition:

    check_some_condition(Input,
        fun() -> success_callback(ArgumentsForCallback) end,
        fun() -> fail_callback(ArgumentsForCallback) end).
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.