lunacer 2020. 5. 1. 11:45

[Background]

Functions are Objects
: In Javascript, functions are first-class objects. 
: can work in the same way with other objects, like assigning them to variables and passing them as arguments into other functions.


[Why use this]

Most of the programs and applications operate in a synchronous manner.
When we don't know when data will be served back(such as an external API), we want to wait for the response, but we don’t want our entire application grinding to a halt while our data is being fetched.
These situations are where callback functions come in handy.
: So... is that mean when using the callback function, the part is becoming asynchronous?
  answer: https://www.freecodecamp.org/news/javascript-callback-functions-what-are-callbacks-in-js-and-how-to-use-them/



1. Also known as a "call-after" function.
2. Any executable code that is passed into other code as an argument.

   : that other code is expected to "call back" (execute) the argument at a later given time.

3. This execution may be immediate as in a synchronous callback
4. or it might happen at a later time as in an asynchronous callback.
5. higher-order function
   :
A function that accepts other functions as arguments. It contains the logic for when the callback function gets executed.  
6. Programming languages support callbacks in different 

 

더보기

[e.g.1] A synchronous callback, as it is executed immediately.

function greeting(name) {
  alert('Hello ' + name);
}

function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}

processUserInput(greeting);
더보기

[e.g.2]

function createQuote(quote, callback){ 
  var myQuote = "Like I always say, " + quote;
  callback(myQuote); // 2
}

function logQuote(quote){
  console.log(quote);
}

createQuote("eat your vegetables!", logQuote); // 1

// Result in console: 
// Like I always say, eat your vegetables!

- createQuote is the higher-order function(which accepts two arguments).
- The logQuote function is being used to pass in as our callback function. 
1. Notice that we are not appending parentheses to logQuote when we pass it in as an argument. 
: This is because we do not want to execute our callback function right away, we simply want to pass the function definition along to the higher-order function so that it can be executed later.
2. Need to ensure that we supply those arguments when executing the callback if it expects arguments.
3. can pass in anonymous functions as callbacks.

createQuote("eat your vegetables!", function(quote){ 
  console.log(quote); 
});

 




[asynchronous callbacks]

: Callbacks are used to continue code execution after an asynchronous operation has completed.
: A good example is
  ↳ the callback functions executed inside a .then() block chained onto the end of a promise after that promise fulfills or rejects.
      This structure is used in many modern web APIs, such as fetch().


https://developer.mozilla.org/en-US/docs/Glossary/Callback_function

https://en.wikipedia.org/wiki/Callback_(computer_programming)

https://www.freecodecamp.org/news/what-is-a-callback-function-in-javascript/

 

What is a Callback Function in JavaScript?

This article gives a brief introduction to the concept and usage of callback functions in the JavaScript programming language. Functions are Objects The first thing we need to know is that in Javascript, functions are first-class objects. As such, we can w

www.freecodecamp.org