为什么承诺 return "undefined" Nodejs Javascript
Why promises return "undefined" Nodejs Javascript
我有这个问题,当我尝试在主函数中创建 Promise 时,returns“未定义”
这是代码
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();
在 asdf 函数中,应该 recipe 来自 pdfDownload 的承诺结果吧?
在“console.log(await pdfDownload({ itemData }))”部分中,我得到“undefined”
这里有两个 return 承诺的函数,pdfDownload()
和 asdf()
。这两个函数都在没有解析值的情况下解析它们的承诺,因此是 undefined
解析值。
pdfDownload()
从这一行得到它的承诺:
fileStream.on("finish", resolve);
但是,当 finish
事件发出时,它没有参数,因此 resolve()
将被调用而没有参数,因此 promise 将以 undefined
作为已解决的值。
asdf()
从 async
声明中得到它的承诺,所以我们看看函数 returns 是什么,因为 return 值将成为承诺 async
函数 returns。但是,没有明确的 return
声明,因此 return 值为 undefined
,因此 asdf()
returns 的承诺也解析为 undefined
值。
不清楚您希望这些承诺使用什么值来解决。您可以通过更改此设置 pdfDownload()
中的解析值:
fileStream.on("finish", resolve);
对此:
fileStream.on("finish", () => {
resolve("all done");
});
并且,您可以通过添加显式 return 值在 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);
});
我有这个问题,当我尝试在主函数中创建 Promise 时,returns“未定义” 这是代码
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();
在 asdf 函数中,应该 recipe 来自 pdfDownload 的承诺结果吧? 在“console.log(await pdfDownload({ itemData }))”部分中,我得到“undefined”
这里有两个 return 承诺的函数,pdfDownload()
和 asdf()
。这两个函数都在没有解析值的情况下解析它们的承诺,因此是 undefined
解析值。
pdfDownload()
从这一行得到它的承诺:
fileStream.on("finish", resolve);
但是,当 finish
事件发出时,它没有参数,因此 resolve()
将被调用而没有参数,因此 promise 将以 undefined
作为已解决的值。
asdf()
从 async
声明中得到它的承诺,所以我们看看函数 returns 是什么,因为 return 值将成为承诺 async
函数 returns。但是,没有明确的 return
声明,因此 return 值为 undefined
,因此 asdf()
returns 的承诺也解析为 undefined
值。
不清楚您希望这些承诺使用什么值来解决。您可以通过更改此设置 pdfDownload()
中的解析值:
fileStream.on("finish", resolve);
对此:
fileStream.on("finish", () => {
resolve("all done");
});
并且,您可以通过添加显式 return 值在 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);
});