callback javascript code example

Example 1: javascript getposts callback

// *** JS GetPosts Callback ***

    run.addEventListener('click', () => {
        window.lib.getPosts((error, articles) => {
          	return (error ? console.log(error) : console.log(articles));
            /* Alternate syntax
          	 if (error) {
                 console.log(error);
             }
             for (elem of articles) {
                 console.log(elem);
            }; // => console.log(articles); similarly works
            /*
        });
    })

Example 2: javascript callback

/*
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. 
*/
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.

Example 3: what is callback in function handler

Events - Think of a Server (Employee) and Client (Boss).
One Employee can have many Bosses.
The Employee Raises the event, when he finishes the task,
and the Bosses may decide to listen to the Employee event or not.
The employee is the publisher and the bosses are subscriber.

Callback - The Boss specifically asked the employee to do a task
and at the end of task done,
the Boss wants to be notified. The employee will make sure that when the task
is done, he notifies only the Boss that requested, not necessary all the Bosses.
The employee will not notify the Boss, if the partial job is done.
It will be only after all the task is done. 
Only one boss requested the info, and employee only posted the 
reply to one boss.

R: https://stackoverflow.com/a/34247759/7573706

Example 4: callback function js

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

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

processUserInput(greeting);

Example 5: callback

/*
tl;dr: Callbacks are a way to force one function to complete,
before running another. Used for asynchronus programming.
*/

// Consider this example:
  
names = ['Anthony', 'Betty', 'Camie']

function addName(name, callback){
	setTimeout(function(){
    	names.append(name)
      	callback()
    }, 200)
}

function getNames(){
	setTimeout(function(){
    	console.log(names)
    }, 100)
}

addName('Daniel', getNames)
/* 

addName('Daniel', getNames) is saying:

'finish the function addName() BEFORE running the function getNames()'

> 'getNames()' here is the callback

Without the call back in this example, getNames would finish 
before addName. (setTimeout is used to force one function to go slower)

Callbacks are given as arguments to other functions

*/

Example 6: what are callback functions

// A function which accepts another function as an argument
// (and will automatically invoke that function when it completes - note that there is no explicit call to callbackFunction)
funct printANumber(int number, funct callbackFunction) {
    printout("The number you provided is: " + number);
}

// a function which we will use in a driver function as a callback function
funct printFinishMessage() {
    printout("I have finished printing numbers.");
}

// Driver method
funct event() {
   printANumber(6, printFinishMessage);
}