FAQ: JavaScript Promises - Chaining Multiple Promises

Probably the confusion is about the difference between parameters and arguments, of functions. resolvedValueArray is just a parameter of an arrow function.

1 Like

let hasEnoughMoney = order.giftcardBalance >= total;

I want to understand how we can access order.giftcardBalance when the order array is app.js and the above line of code is in library.js and there is no importation done from app.js, yet giftcardBalance is accessible.

Good question! I see your point.

Like it was commented above, the issue that is easy to confuse is parameters vs arguments, as well as defining/declaring a function vs invoking/calling a function.



When you declare a function, you’re talking to your new employee, J.S., and explaining them what their task will be, what tools they will need, like: well, JS, you will need an array passed to you, the first item will be an object, and will have a property like this. It is like if you’re explaining the new employee the tasks they will do and the tools they will receive.

Part of these tools that they will receive are called parameters, but for now, you’re not giving them the tools, just explaining them what tools they will receive and how to work with them when the time comes.

Example of declaring a function:

function JSTaskAtWork(tool1, tool2) {
    // ...will have to do this
    // ...take tool1, it will have this feature, use it like that
    // ...take tool2, check that it's clean first
    // ...return a value, for example
}

Or you could also do this like:

const JSTaskAtWork = (tool1, tool2) => {
    // ...same as above here
}


Time passed, J.S. decided to sign the employment contract, and today is the day where the young J.S. will finally start actually working at the company. What changes? Well, now there’s no talking or explaining, now J.S. will have to take the tools and do their job, right?

That is what happens when you call/invoke the function. Calling the function is like being at work and being asked to do that task that they explained you before. Now, there’s no explaining, that’s why the wording is way shorter.

Example:

JSTaskAtWork(actualTool1Here, actualTool2Here);

And now, there’s no explaining the tools/parameters, you only receive the actual tools that you’ll use (the arguments). But these need to be at your disposal, otherwise you cannot do you job now. This is when it would matter to have access to the args.



Now, how does that apply in this case? You may have noticed it already, but anyway let’s analyze it. Well, in library.js the order object is being used only as part of “that explanation” in the definition of the function (on top of that, it is inside a promise, but that’s a whole other thing). It is not actually being used at that moment, only “explained” in the definition of the function. Now, if you take a look at app.js, do you notice the difference? It is called there, not defined. There is no explaining. The function there is being given a tool/value and has to do the work now.

Hope that helped clarify that a little bit further.

1 Like

I get what you mean but how can the giftcardBalance key be accessed from library.js when clearly the order object was declared in app.js. Is it because it was declared in the global scope and therefore can be accessed from another file?

Sorry, I think my example may not have been the best, it’s just the weird way in which I understand things (believe it or not it is :smiley: ) but of course, everyone has different ways to learn.
TLDR:
To be honest, what I think is that in this particular situation, you don’t call the function that uses the order object in the library.js. You only call it in app.js. So, it is in this file when you have to be sure to have access to the order object and thus to its giftcardBalance key.
TLDR ends here



Side note:
To understand it even better, you can try this:
open you dev tools, go to console and write this:

const aFunctionsWithObjects = obj1 => {
    let a = obj1.a.really.really.nested.key.here;  
    return a; 
};

No problem, right? No errors, no issues.

Now, copy that code, open a new tab and open the dev tools console again there. Add this line at the end:

aFunctionsWithObjects(myObj);

Have you received ReferenceError? Why? myObj is not defined it may say.

You are trying to call the function, hence the arg is checked (the param not).

1 Like

Ohhhhh okay I understand now, thanks for your help. Your example was great btw and I appreciate it. The function was called in app.js, thats the answer I needed. Thanks once more.

1 Like

Hello! Not a simple topic. That’s why I don’t understand why it’s presented through such a complicated example … It’s much easier to understand the promise than to break down the elements of the example. There are many videos on youtube that are more illustrative. e.g. https://youtu.be/670f71LTWpM Sorry for the criticism, but otherwise it is not typical of the learn materials of Codecamy. I still like you :kissing_heart:
(e.g. it would be more understable:

let inStock = itemsArr.every(orderItem => store[orderItem[0]].inventory >= orderItem[1]);