从作为非匿名函数参数的异步匿名函数返回数据

问题描述 投票:0回答:1

我正在尝试返回异步匿名函数的内容参数。内容参数填充在匿名函数块中,但返回时它是未定义的。

我尝试等待整个事情没有效果:

  const file = 
     await storage.bucket(bucketName).file("myImage.jpg").download(async 
        function(err, contents) {
          return contents;
     });
 <file is undefined>

我也尝试等待内容:

return await contents;
,结果相同。

这是在 GCP 云函数中,但我确信我只是搞砸了这里的 JS。

提前感谢您的指导。

javascript google-cloud-platform async-await anonymous-function
1个回答
0
投票

您似乎没有正确使用 API。如果您要使用

download()
返回的 Promise,则无需传递回调函数。

如果要将存储对象的内容下载到内存中,其工作原理如下:

    const contents = await storage.bucket(bucketName).file(fileName).download();

    console.log(
      `Contents of gs://${bucketName}/${fileName} are ${contents.toString()}.`
    );

这直接取自来自 Google 的示例代码

© www.soinside.com 2019 - 2024. All rights reserved.