Home > database >  Why promises return "undefined" Nodejs Javascript
Why promises return "undefined" Nodejs Javascript

Time:04-07

i have this problems, when i try to create a Promises, in the main function, returns "undefined" here is the code

export const pdfDownload = async ({ itemData }) => {
    let today = new Date();
    const { client_number, document_date, document_number, document_url, dx } = itemData
    let url = document_url
    let path = "./files/Downloads/"   dx   "/"   today.getDate()   "-"   (today.getMonth()   1)   "-"   today.getFullYear()   "/"
          client_number   "-"   document_date   "-"   today.getDate()   "-"   (today.getMonth()   1)   "-"   today.getFullYear()   "-"   document_number   ".pdf";
    const res = await fetch(url);
    const fileStream = fs.createWriteStream(path);

    return new Promise((resolve, reject) => {
        res.body.pipe(fileStream);
        res.body.on("error", reject);
        fileStream.on("finish", resolve);
    })

}
const asdf = async () => {
  const itemData = {
    client_number: "holaaaa",
    document_date: 'asdf',
    document_number: 3,
    document_url: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
    dx: "test"
  }
  console.log(await pdfDownload({ itemData }))

}
asdf();

in asdf function, should recibe the result of the promise from pdfDownload right? in the part "console.log(await pdfDownload({ itemData }))" is where i get "undefined"

CodePudding user response:

You have two functions here that return a promise, pdfDownload() and asdf(). Both of those function resolve their promise with no resolved value, thus an undefined resolved value.

pdfDownload() gets its promise resolved from this line:

fileStream.on("finish", resolve);

But, when the finish event is emitted, it has no arguments, thus resolve() will be called with no arguments so that promise will be resovled with undefined as the resolved value.

asdf() gets its promise from the async declaration so we look to see what the function returns because that return value will become the resolved value of the promise that the async function returns. But, there is no explicit return statement so the return value is undefined and thus the promise that asdf() returns also resolves to an undefined value.

It's not clear what values you want these promises to resolve with. You can set a resolved value in pdfDownload() by changing this:

fileStream.on("finish", resolve);

to this:

fileStream.on("finish", () => {
    resolve("all done");
});

And, you can set a resolved value in asdf(), by adding an explicit return value:

const asdf = async () => {
  const itemData = {
    client_number: "holaaaa",
    document_date: 'asdf',
    document_number: 3,
    document_url: "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
    dx: "test"
  }
  let result = await pdfDownload({ itemData });
  console.log(result);
  return result;    
}

asdf().then(result => {
   console.log(`Got result = ${result}`);
}).catch(err => {
   console.log(err);
});
  • Related