为什么承诺返回“未定义” Nodejs JavaScript
我有这个问题,当我尝试创建 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您这里有两个函数,可以返回承诺,
pdfdownload()
和asdf()
。这些功能都可以解决他们的承诺而没有解决价值,因此不确定的
解决值。pdfdownload()
从这一行中得出其诺言:但是,当
完成
事件发出时,它没有参数,因此resolve()
>将在没有参数的情况下被调用,以便将承诺用不确定的
作为解决值解决。ASDF()
从async
声明中获得承诺异步函数返回。但是,没有明确的返回
语句,因此返回值是undefined
,因此承诺asdf()
返回也解析为未定义的
值。目前尚不清楚您想要这些诺言要解决的价值。您可以通过更改以下方式在
pdfdownload()
中设置一个已解析值:对此:
和,您可以通过添加一个显式返回值来设置
asdf()
中的解决值:You have two functions here that return a promise,
pdfDownload()
andasdf()
. Both of those function resolve their promise with no resolved value, thus anundefined
resolved value.pdfDownload()
gets its promise resolved from this line:But, when the
finish
event is emitted, it has no arguments, thusresolve()
will be called with no arguments so that promise will be resovled withundefined
as the resolved value.asdf()
gets its promise from theasync
declaration so we look to see what the function returns because that return value will become the resolved value of the promise that theasync
function returns. But, there is no explicitreturn
statement so the return value isundefined
and thus the promise thatasdf()
returns also resolves to anundefined
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:to this:
And, you can set a resolved value in
asdf()
, by adding an explicit return value: