S
S
seredaes2015-10-22 16:36:51
JavaScript
seredaes, 2015-10-22 16:36:51

Code as javaScript function parameters?

Hello.
I foresee the answer that I searched badly in Google.
But at least I tried :)

I want to write a function that will take 2 parameters:
1. variable(s)
2. Javascript code.

So that for example I could call my function like this:
fun1("Hi", function(data) {
here any number of lines of JavaScript code
and would be executed only after the work is completed or the result of
the fun1 function has arrived
});

I'll never know how to do it ...
Tell me or point your finger at the theory so that it's clear :)
thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivanq, 2015-10-22
@Ivanq

That is, it is necessary that the function be called only after the parent function has been executed? Well, it's unlikely.
Could be something like this:

(function(data) {
  dosomething();
})(data = fun1("Привет"));

V
Vitaly Inchin ☢, 2015-10-22
@In4in

You asked yourself, you answered...

function fun1(d, f){
   //code
  f(d);
}

fun1("Привет", function(data){
  alert(data);
});

N
Nikita K., 2015-10-23
@bonilka

To the above, I will add that google "javascript callback function".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question