javascript - what is wrong with my callback? -


this file "path-info.js" has 2 functions: pathinfo & callback. pathinfo collects info file path in object "info", callback gets object , returns it. code:

"use strict"; const fs = require("fs");  let getinfo = function (err, someobject) {     if (err) throw err;     return someobject; };  function pathinfo(path, callback) {   let info = {};    info.path = path; // write path info    fs.stat(path, (err, type) => { // write type info     if (type.isfile()) {       info.type = "file";     }     if (type.isdirectory()) {       info.type = "directory";     } else {       info.type = "undefined";     }   });    fs.stat(path, (err, type) => { // write content info if file     if (type.isfile()) {       fs.readfile(path, "utf8", (err, content) => {         info.content = content;       });     } else {       info.content = "undefined";     }   });    fs.stat(path, (err, type) => { // write childs if directory     if (type.isdirectory()) {       fs.readdir(path, (err, childs) => {         info.childs = childs       });     } else {       info.childs = "undefined";     }   });    getinfo(null, info); // callback returns object "info" }  module.exports = pathinfo; 

i use callback function shown, instance, here: nodejs callbacks simple example. still, code not work , not why.

i call code using file "test.js", here code:

const pathinfo = require('./path-info'); function showinfo(err, info) {   if (err) {     console.log('error occurred');     return;   }    switch (info.type) {     case 'file':       console.log(`${info.path} — file, contents:`);       console.log(info.content);       console.log('-'.repeat(10));       break;     case 'directory':       console.log(`${info.path} — directory, child files:`);       info.childs.foreach(name => console.log(`  ${name}`));       console.log('-'.repeat(10));       break;     default:       console.log('is not supported');       break;   } }  pathinfo(__dirname, showinfo); pathinfo(__filename, showinfo); 

so logic need give callback object contains info directory or file. depending on that, console.logs displayed.

any appreciated!

upd: updated code, renamed "callback" function "getinfo".

a callback function pass parameter function.

in case, second parameter function showinfo callback. function pathinfo accepts 2 parameters, second showinfo.

so when call it, execute code within showinfo parameters, err, else.

in case, name second parameter "callback" in showinfo, have execute asked parameters (err , info).

example:

function myfunc (parameter,cb) {     cb(null,{}); }  myfunc("one", function (err,res) {     console.log(err); }); 

where "cb" in "myfunc" function sent second parameter.

it can write did it:

var cb = function (err,res) {     console.log(err); }  myfunc("one",cb); 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -