【问题标题】:how to change the function declaration to change the promise handling in node.js?如何更改函数声明以更改 node.js 中的承诺处理?
【发布时间】:2019-12-16 17:25:36
【问题描述】:

我是 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')

此代码给出以下错误:

(node:10708) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝。此错误源于在异步内部抛出 在没有 catch 块的情况下运行,或者通过拒绝一个承诺 不使用 .catch() 处理。 (拒绝 ID:1)(节点:10708)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。

我需要用不同的方式重写函数吗?

【问题讨论】:

  • downloadFile 的调用者应该.catch 它可能产生的错误
  • 你可以为你建议的更改显示一些示例代码吗?

标签: javascript node.js


【解决方案1】:

对于async 操作,如果出现任何error,则应添加回退,您可以在method 中添加try/catch 块 异步函数 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]
  }

【讨论】:

    【解决方案2】:

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

    如果使用await 调用函数,所有可能拒绝其承诺的异步函数都必须有一个错误处理程序,例如.catch()try/catch


    您还可以从源头捕获错误并确保downloadFile() 不能拒绝它的承诺。无论哪种方式,您都必须在某个地方捕获错误,并且不要让被拒绝的承诺得不到处理。 Promise 和 node.js 的设计者都认为未处理的被拒绝的 Promise 应该类似于未处理的异常(因为它们在逻辑上非常相似 - 被拒绝的 Promise 是异步代码的异常)。

    【讨论】:

    • 感谢修改函数调用语句,我猜该方法给出了误导性错误;说明谷歌帐户处于非活动状态,但是当我尝试列出存储桶中的文件时,效果很好,没有任何错误。
    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 2011-01-09
    • 2019-09-25
    • 2016-03-01
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2015-12-28
    相关资源
    最近更新 更多