为什么承诺返回“未定义” Nodejs JavaScript

发布于 2025-01-19 07:29:22 字数 1232 浏览 1 评论 0原文

我有这个问题,当我尝试创建 Promises 时,在主函数中返回“未定义” 这是 asdf 函数中的代码

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();

,应该记录 pdfDownload 的承诺结果吗? 在“console.log(await pdfDownload({ itemData }))”部分中,我得到“未定义”

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身软味 2025-01-26 07:29:22

您这里有两个函数,可以返回承诺,pdfdownload()asdf()。这些功能都可以解决他们的承诺而没有解决价值,因此不确定的解决值。

pdfdownload()从这一行中得出其诺言:

fileStream.on("finish", resolve);

但是,当完成事件发出时,它没有参数,因此resolve()>将在没有参数的情况下被调用,以便将承诺用不确定的作为解决值解决。

ASDF()async声明中获得承诺异步函数返回。但是,没有明确的返回语句,因此返回值是undefined,因此承诺asdf()返回也解析为未定义的值。

目前尚不清楚您想要这些诺言要解决的价值。您可以通过更改以下方式在pdfdownload()中设置一个已解析值:

fileStream.on("finish", resolve);

对此:

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

和,您可以通过添加一个显式返回值来设置asdf()中的解决值:

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);
});

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);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文