fonction avec callback js 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

// Create a callback in the probs, in this case we call it 'callback'
function newCallback(callback) {
  callback('This can be any value you want to return')
}

// Do something with callback (in this case, we console log it)
function actionAferCallback (callbackData) {
  console.log(callbackData)
}

// Function that asks for a callback from the newCallback function, then parses the value to actionAferCallback
function requestCallback() {
  newCallback(actionAferCallback)
}