How does an argument gets passed to a function without explicitly sending the argument to the parameter?

In this scenario, at the bottom, I’m seeing that the result of the promise will be passed as an argument to the “resolvedValue” parameter of the handleSuccess function but I don’t understand why and how that works.

const prom = new Promise((resolve, reject) => {
const handleSuccess = (resolvedValue) => {
prom.then(handleSuccess); // Prints: 'Yay!'
1 Like

Hi there.
You have created a promise that resolves to a string ‘Yay!’. When you chain .then() with your handleSuccess function as a callback function, you tell javascript “call this function, when my promise is resolved”. That’s why javascript takes your resolved value (‘Yay!’) and passes it as an argument to the callback function.

1 Like

My confusion was on how the callback functions worked I needed to revisit this topic :wink: and the lack of visibility of what happens when you call “.then” because in this example it’s obvious on the result passed as argument to the callback function:

function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;

function myCalculator(num1, num2, myCallback) {
  let sum = num1 + num2;

myCalculator(5, 5, myDisplayer);

here when function “myCalculator” passes “myDisplayer” as an argument, you can see that inside “myCalculator” the variable sum is being passed as an argument for the callback function “myDisplayer”.