Bruce Gust
asked on
Am I explaining this correctly?
Here's my function:
fetchAll looks like this:
...and getProductsFromFile looks like this:
Tell me, please, if I'm explaining this correctly. I'm attempting to understand this stuff to the degree when I can explain it to others and I'm having a hard time.
What I've been taught / told is that I'm looking at an expression that's being passed as an argument to another function. That's the quintessential advantage / utility of a callback.
So, this:
...is being passed as an argument to:
BTW: An expression represents the "guts" of a function, correct?
Thing is, if I try to envision that based on face value, I see something like this:
That seems both convoluted and bogus.
I'm thinking that for all intents and purposes, the "getProductsFromFile" method would be written like this:
Instead of:
All the examples I've seen have very basic functions being used to illustrate what a callback is. This is far more complex and, as a result, seems a little more complicated.
What I'm "smelling" is that when you pass this:
...as an argument into:
...you're looking at "products" as being a variable that's ultimately going to hold the value coming back from "getProductsFromFile."
It's tempting, perhaps, to look at the entire expression associated with the "products" variable as somehow being processed by the "getProductsFromFile" method.
It's not.
Rather, "products" is the variable that's holding the value coming from "getProductsFromFile" and that's what's going to be forwarded to the "product-list.ejs" file.
Is that right?
exports.getProducts = (req, res, next) => {
Product.fetchAll(products => {
res.render("shop/product-list", {
prods: products,
pageTitle: "All Products",
path: "/products"
});
});
};
fetchAll looks like this:
static fetchAll(cb) {
getProductsFromFile(cb);
}
...and getProductsFromFile looks like this:
const getProductsFromFile = cb => {
fs.readFile(p, (err, fileContent) => {
if (err) {
return cb([]);
}
cb(JSON.parse(fileContent));
});
};
Tell me, please, if I'm explaining this correctly. I'm attempting to understand this stuff to the degree when I can explain it to others and I'm having a hard time.
What I've been taught / told is that I'm looking at an expression that's being passed as an argument to another function. That's the quintessential advantage / utility of a callback.
So, this:
products => {
res.render("shop/product-list", {
prods: products,
pageTitle: "All Products",
path: "/products"
}
...is being passed as an argument to:
static fetchAll(cb) {
getProductsFromFile(cb);
}
BTW: An expression represents the "guts" of a function, correct?
Thing is, if I try to envision that based on face value, I see something like this:
static fetchAll(products => {
res.render("shop/product-list", {
prods: products,
pageTitle: "All Products",
path: "/products"
}) {
getProductsFromFile(products => {
res.render("shop/product-list", {
prods: products,
pageTitle: "All Products",
path: "/products"
});
}
That seems both convoluted and bogus.
I'm thinking that for all intents and purposes, the "getProductsFromFile" method would be written like this:
getProductsFromFile(products)
Instead of:
products => {
res.render("shop/product-list", {
prods: products,
pageTitle: "All Products",
path: "/products"
});
}
All the examples I've seen have very basic functions being used to illustrate what a callback is. This is far more complex and, as a result, seems a little more complicated.
What I'm "smelling" is that when you pass this:
products => {
res.render("shop/product-list", {
prods: products,
pageTitle: "All Products",
path: "/products"
}
...as an argument into:
static fetchAll(cb) {
getProductsFromFile(cb);
}
...you're looking at "products" as being a variable that's ultimately going to hold the value coming back from "getProductsFromFile."
It's tempting, perhaps, to look at the entire expression associated with the "products" variable as somehow being processed by the "getProductsFromFile" method.
It's not.
Rather, "products" is the variable that's holding the value coming from "getProductsFromFile" and that's what's going to be forwarded to the "product-list.ejs" file.
Is that right?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
As always, thanks for taking the time to elaborate on the "why" and not just the "what."
I'm explaining this back to you...
The reason I was having a hard time is because of the way I was processing
Open in new window
...specifically when we hit the "getProductsFromFile" piece:
Open in new window
When we got to that code, that's where it became confusing to me because was looking at the "cb" code like this:
Open in new window
Despite having read and studied and written out several basic examples of callbacks, I was still drawn to the idea that "products" was the name of the function and not the variable that function was expecting to process.
That's why when we hit the "getProductsFromFile" portion, I was confused because I couldn't see how the parsed json file was suddenly going to be "products." But after having read through your explanation, I now see where my perspective was flawed and I can't tell you just how many light bulbs are going off right now.
I'm slowly making my way through a Udemy tutorial and I'm forcing myself to "parK" and be certain that I'm understanding the concepts being covered and not just copying and pasting. This was one of those areas where I was a little hesitant.
I'm closing the question and giving you the points. If you don't mind, however, what is the correct verbiage for what's happening in this scenario as far as...
I'm passing a function as an argument into the fetchAll function or...
I'm passing an expression as an argument into the fetchAll function
Which is it and why?
There's a lot of info out that isn't always especially clear,
Thanks, Julian!