Do Promises accept arguments or parameters?

I have come across many videos and articles stating that Promises use arguments instead of parameters and have trouble understanding that.

Looking at this example

const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('foo');
  }, 300);
});

Where the new Promise accepts (resolve , reject) I know that if those are changed to other words then the result would still be the same.

For instance if I changed it to :

const myPromise = new Promise((anyOtherWord1,anyOtherWord2) => {
  setTimeout(() => {
    anyOtherWord1('foo');
  }, 300);
});

then it would still get the same result. So my question is , wouldnt this be a parameter then since there seems to be an inherent function being used regardless of what its named?

Parameters can be whatever you want to call them. In all functions, whether it is a “normal” function of here, where it is a callback function in the constructor of a Promise. There is nothing odd about that.

If I have a function like:

const add = (x, y) => x + y

I can change it to:

const add = (elephantPajamas, appleMonday) => elephantPajamas + appleMonday

JS doesn’t care. But if you can it’s good to label them for what they are/do. With the Promise callback the convention is to call them that because that is an excellent descriptor of their purpose. But it is not hardcoded into JS to call them that. But it is a very strong convention and you might get some weird looks.

I hope you know what exactly argument and parameter mean.

Argument is the passing value and parameter is just the receiving variable , meaning argument is the actual value you are passing to the function when the function get called.

someFun(6,10) here 6 and 10 are arguments. While parameter is the variable that receives this value in the function declaration.

function someFun(num1,num2){
...........
}

here num1 and num2 are parameters which will get assigned by whichever value you passed as arguments

2 Likes

Yeah, that is a good point too, I missed that.

1 Like

So if I am understanding correctly then they are actually parameters then.

Agreed , which is why I had some confusion when some articles and videos refer to them as arguments. Because with both of our examples the words can be changed to anything , as they are just a placeholder.

Similar to how the (resolve , reject ) in my example can be changed to (anyOtherWord1,anyOtherWord2) is how your example

function someFun(num1,num2){
...........
}

can be changed to

function someFun(num3,num4){
...........
}

and still get the same result as long as the code in the blocks are the same.

1 Like

Right, you are changing the parameter (the label in the function), but the argument (value) ends up being the same.

2 Likes

Yeah consider them as just variable declarations but the value is assigned by javascript under the hood. You can name your variable whatever you want in the end only the data that holds matters

1 Like

It’s like in geometry, a parabola is y = x^2. I can rename those, as long as I rename the axises as well. “x” and “y” are the parameters and the values that will be pumped into the formula are the arguments.

2 Likes

Thank you both, I know this may seem like a small issue but it was driving me crazy , and none of the sites or videos I viewed would clarify.

Thanks again

1 Like

Yeah, we all have little things like that as we learn. Glad we were able to help. Pay it forward.

Thank you both, I know this may seem like a small issue but it was driving me crazy , and none of the sites or videos I viewed would clarify.

Thanks again

1 Like

Would you link to any of the videos saying that, just out of curiosity.

My take would be…

//put the function separately
function myFn(a, b) {
  ...
}
//or const myFn = (a,b)=> ...
new Promise(a = myFn)
  • It doesn’t matter what you call myFn.
  • myFn is an argument.
  • Specifically, it is an argument to the constructor inside the Promise Class.
  • They don’t call it a but something else, but the assignment occurs anyways.

In maths, someone told me that once a function is defined, parameters are constant, but arguments aren’t: sum(a=5,b=7); sum(7,8) etc.

Which is just a good way to remember it.

It was a lot of random websites , and YouTube videos that would take a long time to go through and find. I dont have the time currently , but if I get the chance I can send a direct message to you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.