[英]how to change the function declaration to change the promise handling in node.js?
我是 node.js 的新手,正在使用以下代码将 csv 文件从 GCS 存储桶下载到本地文件夹。
// 从 GCS 存储桶下载文件的函数
async function downloadFile(bucketName, srcFilename, destFilename) {
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client from a Google service account key.
const storage = new Storage({keyFilename: "keyFile.json"});
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const srcFilename = 'Remote file to download, e.g. file.txt';
// const destFilename = 'Local destination for file, e.g. ./local/path/to/file.txt';
const options = {
// The path to which the file should be downloaded, e.g. "./file.txt"
destination: destFilename,
};
// Downloads the file
await storage
.bucket(bucketName)
.file(srcFilename)
.download(options);
console.log(
`gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`
);
// [END storage_download_file]
}
// call downloadFile function
downloadFile('temp_bucket123','test_data.csv','./gcs_download/test_data.csv')
此代码给出以下错误:
(节点:10708) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 1) (node:10708) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。 将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。
我需要以不同的方式重新编写函数吗?
看起来downloadFile()
内部的某些东西正在创建一个错误,导致从downloadFile()
返回一个被拒绝的承诺。 该警告是因为您没有对此进行错误处理,并且未处理的拒绝被认为是邪恶的。 正如错误所说,将来它们甚至可能会自动终止您的 node.js 进程。
downloadFile('temp_bucket123','test_data.csv','./gcs_download/test_data.csv').then(() => {
console.log("done with download now");
}).catch(err => {
console.log(err);
});
所有有任何方式可能拒绝其承诺的异步函数都必须有一个错误处理程序,例如.catch()
或如果使用await
调用函数,则使用try/catch
。
您还可以在源头捕获错误并确保downloadFile()
不会拒绝其承诺。 无论哪种方式,您都必须在某处捕获错误,不要让被拒绝的承诺得不到处理。 promises 和 node.js 的设计者都决定一个未处理的被拒绝的 promise 应该类似于一个未处理的异常(因为它们在逻辑上非常相似 - 被拒绝的 promise 是异步代码的异常)。
对于async
操作,如果发生任何error
,您应该添加回退,您可以在method
添加try/catch
块 async function downloadFile(bucketName, srcFilename, destFilename) {
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');
// Creates a client from a Google service account key.
const storage = new Storage({keyFilename: "keyFile.json"});
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const bucketName = 'Name of a bucket, e.g. my-bucket';
// const srcFilename = 'Remote file to download, e.g. file.txt';
// const destFilename = 'Local destination for file, e.g. ./local/path/to/file.txt';
const options = {
// The path to which the file should be downloaded, e.g. "./file.txt"
destination: destFilename,
};
// Downloads the file
try {
await storage
.bucket(bucketName)
.file(srcFilename)
.download(options);
console.log(
`gs://${bucketName}/${srcFilename} downloaded to ${destFilename}.`
);
} catch (err) {
console.log('Some error occured', err)
}
// [END storage_download_file]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.