0

I've created a promise function that returns error TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received undefined

 async function DlFile(url, fileName) {
  return new Promise ((fulfill, reject) => {
      https.get(url, function(res){
        const fileStream = fs.createWriteStream(`C:\\Users\\Garingo-PC\\Documents\\${fileName}.pdf`);
        res.pipe(fileStream);
    
        fileStream.on("error", reject);
    
        fileStream.on("finish", fulfill({
          status: 'ok'
        }));
      });
    }); 
  
}

const Main = async () => {
  const result = await DlFile(url, filename);
  console.log(result); //returns undefined
}

it should return some an object if it finished downloading the file

1 Answer 1

1

fileStream.on("finish")'s param supposed to be a function, but you're not entering a function into it. fulfill is a callback but you've called it before you pass it into the finish callback. The correct way to use fulfill is that you have to call it when finish event is triggered. Like so:

fileStream.on("finish", () => {
    fulfill({ // Call when finish event being fired
      status: 'ok'
    })
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.