Jump to:

Our volunteers haven't translated this article into ไทย yet. Join us and help get the job done!
You can also read the article in English (US).

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.

Here is a quick example:

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

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

processUserInput(greeting);

The above example is a synchronous callback, as it is executed immediately.

Note, however, that callbacks are often used to continue code execution after an asynchronous operation has completed — these are called asynchronous callbacks. 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().

Learn more

General knowledge

Document Tags and Contributors

ป้ายกำกับ: 
ผู้มีส่วนร่วมกับหน้านี้: mdnwebdocs-bot, avtraino, chrisdavidmills, aijaz-ali-abro, sideshowbarker, Sheldon01, qq184872515, carmenica, mehdi2237, netpoetica, PushpitaPikuDey, johnwatsondev
อัปเดตล่าสุดโดย: mdnwebdocs-bot,