Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
What advantage(s) do some built-in methods provide by taking a synchronous callback as their argument?

I'm trying to understand callbacks thoroughly and this is the fundamental missing piece. I've scoured the internet for this answer but most just talk about async callbacks.

Q 1: Why do some built-in methods like Array.prototype.forEach() use synchronous callbacks? What advantage does this feature provide?

Q 2: How do we decide it's time to use a synchronous callback when implementing custom methods of our own?

Background:

javascript's for of loop functionally does the exact same thing as its forEach() counterpart when talking about arrays. They both loop from the front to the end of the array. So why do we need both?

Two obvious differences (in general) I see are:

  1. for of loop can loop over any iterable, forEach is specific for arrays.
  2. for of loop allows us to use continue and break keywords, forEach() does not
  3. for of loop does NOT use a callback, forEach() DOES use a synchronous callback.

I see the importance of points 1 and 2, but I don't get point 3

Q: What advantage does forEach() provide by taking a synchronous callback as its argument?

An answer other than "it allows us to apply a function to each element" or allows us to "abstract away the logic" would be really helpful unless the answers are actually really just this obvious and I'm overthinking this.

Hope my question is unambiguous and super clear

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Callbacks aren't inherently synchronous or asynchronous. A callback is just a function that was given as an argument to another function.

When that callback gets called, is up to function and typically some other event precedes that. If that event is itself asynchronous, then the call is also asynchronous.

This is completely separate from forEach and a for. The reason both exists, is probably a combination of the following:

  • for(.. of ..) wasn't a thing for a long time, and for(.. in ..) has some surprising behavior related to prototypes. This led to many libraries implementing a functional (for)each, most notably jQuery.
  • Another jQuery-era reason why people liked jQuery each, is because lots of folks got confused around how variables behaved in scope/closures and loops. Doing a functional version makes it much easier to not have closure variables overwritten for previous iterations.
  • Lastly, some people just like the idea using functions for loops.

Especially the first two reasons made $.each popular, to a point where a native forEach was added to the language.

There's fewer reasons to use forEach today, because:

  • forEach doesn't work with await.
  • We now have block-scoped let and const so variables overwriting after each iteration of the loop is no longer an issue.
  • for(... of ..) does what people expects (no need for hasOwnProperty).
  • You can't 'break' with forEach

Some people still opt to using forEach if the above bullets aren't a concern. I don't fully understand that, but you can't really argue style.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda