Reputation: 377
How can I describe what parameters my function requires and make them viewable when i type my code?
Currently the code in my Product model for this function looks like this. How can I define the callback function in a way that it would hint what it returns?
static findById(id, cb1) {
fs.readFile("./src/database/products.json", (err, data) => {
if (err) {
cb1(err, product);
} else {
const products = JSON.parse(data);
const product = products.find(p => p.id == id);
cb1(err, product);
}
});
};
Upvotes: 0
Views: 717
Reputation: 377
Alright guys thanks for the help! It seems that just by using jsdocs without typescript is enough. I was not aware of how jsdocs works in vscode. After changing my code to the one bellow I got what I wanted. I will be playing with this more.
/**
* Finds a `product` with the given `id` and executes a callback `fn` containing the result.
* @param {number} id
* @param {(err : Error, product: string) => void} callback
*/
static findById(id, callback) {
fs.readFile("./src/database/products.json", (err, data) => {
if (err) {
callback(err, product);
} else {
const products = JSON.parse(data);
const product = products.find(p => p.id == id);
callback(err, product);
}
});
};
Upvotes: 1
Reputation: 575
Use :Javascript documentation starndards
OR You need to use Typescript for define functions interface:
static findById(id: number, cb1: (err: {}, product: {}) => void) {
fs.readFile("./src/database/products.json", (err, data) => {
if (err) {
cb1(err, product);
} else {
const products = JSON.parse(data);
const product = products.find(p => p.id == id);
cb1(err, product);
}
});
};
Upvotes: 1
Reputation: 136
This question might help you : jsdoc and vscode: Documenting a function passed as an argument to another function
Upvotes: 2