0

Friends, help with the task.

I need to write a function that takes the names of two files and calls the function passed in the third parameter and passes it the sum of their sizes as the first argument.

To get the file frame, you need to use the getFileSize (filename, cb) function.

Here is the data:

let fileSizes = {
  testFile1: 65,
  testFile2: 48,
}

function getFileSize(filename, cb) {
  setTimeout(() => cb(fileSizes[filename]), Math.random() * 500);
}

function sumFileSizes(filename1, filename2, cb) {
  //**code here**
}

I wrote a solution that passes the tests. But I don't like it.

Here it is:

function sumFileSizes(filename1, filename2, cb) {
    getFileSize(filename1, (size1)=> {
        getFileSize(filename2, (size2)=> {
            cb(size1 + size2);
        });
    })
}

Can you write something better and shorter? And without using a promise or await .

I'll be very thankful

Art
  • 23
  • 6
  • ... actually this code is completely wrong (not wrong, but who would write code like that...) because of the setTimeout to delay. – user202729 Dec 08 '21 at 11:48
  • Why "not promise or await" exactly? Homework requirement? // Okay looks like it's supposed to test *your* understanding of how callback functions work. Looks like you didn't completely understand it. – user202729 Dec 08 '21 at 11:48
  • @user202729 Yes, this is a requirement. And I agree, this is bad code – Art Dec 08 '21 at 11:52
  • Is it a requirement that you must run both parallelly, or is it okay if they're run sequentially? (Sequential is easier, just nest the callback) -- There's also [Idiomatic way to wait for multiple callbacks in Node.js - Stack Overflow](https://stackoverflow.com/questions/5172244/idiomatic-way-to-wait-for-multiple-callbacks-in-node-js) – user202729 Dec 08 '21 at 11:56
  • Also, *don't* abuse tags. Read the tag info page ['async.js' tag wiki - Stack Overflow](https://stackoverflow.com/tags/async.js/info) to see what it's about before adding the tag. – user202729 Dec 08 '21 at 11:57
  • @user202729 There are no other requirements, just what I wrote earlier – Art Dec 08 '21 at 12:01
  • I fail to understand the use of setTimeout here... – E-telier Dec 08 '21 at 13:33
  • @user202729 I edited, please see. Is this a correct decision, or can it be done better? – Art Dec 08 '21 at 19:13
  • @E-telier I edited, please see. Is this a correct decision, or can it be done better? – Art Dec 08 '21 at 19:13
  • @Art Much better indeed. The requirements feel a bit unnatural and your `sumFileSizes` is a bit difficult to read, but it works and looks correct to me. – E-telier Dec 08 '21 at 19:34

1 Answers1

0

Not sure if you want something like this or not but I believe you can return Promise with size of given filename, and in handler calculate the size.

Here in your example, I have created sumFileSizes where you will get the final number. getFileSize returns new promise which you can use it in sumFileSizes method for each file.

See the Snippet below:

let fileSizes = {
  testFile1: 65,
  testFile2: 48,
}

sumFileSizes("testFile1", "testFile2", getSum).then(_sum => {
    console.log(_sum);
})


function getFileSize(fileName){
  return new Promise((resolve, reject) => {
     resolve(fileSizes[fileName]);
  });
}

function sumFileSizes(fileName1, fileName2, fn){
    return getFileSize(fileName1).then((size1)=> {
      const _total =  getFileSize(fileName2).then((size2)=> {
          return fn(size1, size2);
      });
      
      return _total;
  });
}

function getSum(a,b){
    return a+b;
}




/*function getFileSize(filename, cb) {
  cb(fileSizes[filename]);
}

function sumFileSizes(filename1, filename2, cb) {
  //**code here**
}

function sumFileSizes(filename1, filename2, cb) {
    getFileSize(filename1, (size1)=> {
        getFileSize(filename2, (size2)=> {
            cb(size1 + size2);
        });
    })
}*/

You can test it here also.

Nimitt Shah
  • 4,477
  • 2
  • 10
  • 21