thuynt13
thuynt13

Reputation: 41

Is a callback also known as a higher-order function?

I'm trying to understand callback and higher-order functions but there's a description from a blog post, Understand JavaScript Callback Functions and Use Them, that confuses me, the implication being that they are one and the same:

A callback function, also known as a higher-order function,...

It was repeated verbatim on a Quora answer to the question asking about What is a simple explanation of higher order functions and callbacks in JavaScript?.

It doesn't make sense to me. From what I understand, a higher-order function takes in or returns other functions and a callback function is the function being passed/taken in, so how can it be both at the same time? Is there something that I'm not understanding about that description?

Upvotes: 3

Views: 1027

Answers (3)

Carcigenicate
Carcigenicate

Reputation: 45745

No, a callback is not necessarily a Higher Order Function. They can be though. You could have a callback that accepts another function as an argument.

Callbacks are given to Higher Order Functions, which may be what's leading to the confusion. A function accepting another function as an argument is one of the criteria that causes it to be classified as Higher Order.

Upvotes: 2

Adam Małek
Adam Małek

Reputation: 311

In my opinion, higher order function is a function that takes another function and uses it to abstract some behaviour, e.g. this c# extension method:

    public static IEnumerable<T> OrderByProperty<T>(this IEnumerable<T> items, Func<T, object> selector)
    {
        return items.Select(x => new { o = selector(x), item = x })
                    .OrderBy(x => x.o)
                    .Select(x=> x.item);
    }

it takes a function determining, which property take into account while sorting collection. Sample usage:

    var orderedByA = Enumerable.Range(0, 100)
          .Select(x=> new Item{
            A = x,
            B = 100 - x
          })
          .OrderByProperty(x => x.A);

Callbacks on the other hand can be used to continue application flow when some async or long action is necessary, e.g.

void FirstAsync(){
    Task.Run(()=>{
        Thread.Sleep(1000);
        Console.WriteLine("First executed");
    });
}

void Second()
{
    Console.WriteLine("Second executed");
}

void FirstV2(Action callback)
{
    Task.Run(() =>
    {
        Thread.Sleep(1000);
        Console.WriteLine("First executed");
        callback?.Invoke();
    });
}

void Main()
{
    FirstAsync();
    Second();
    Thread.Sleep(2000);
    FirstV2(Second);
}

Output of program above will be like:

Second executed
First executed
First executed
Second executed

Upvotes: 1

kockburn
kockburn

Reputation: 17616

Callback function

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.

Return a function

A function that returns a function called Higher-Order Function

A callback function is not a Higher-Order Function, unless it is a function that returns a function.

Simple callback:

function toto(callback){
  /** some routine or action before */
  callback();
}

function foo(){
  console.log("I'm a simple callback");
}

toto(foo);

Simple Higher-Order Function

function toto(){
  console.log("I'm a simple Higher-Order Function")
  return function(){
     console.log("I'm the return function");
  }
}

//first log
const func = toto();
//second log
func();

Callback that is also a Higher-Order Function:

function toto(callback){
  /** some routine or action before */
  const func = callback();
  func();
}

function foo(){
  console.log("I'm a callback and Higher-Order function");
  
  return function(){
    console.log("Do something...");
  };
}

toto(foo);

Upvotes: 6

Related Questions