javascript - 在 Windows 10 中使用 NodeJS 在本地运行异步函数

标签 javascript node.js google-cloud-platform async-await environment-variables

我正在努力运行从 Google 示例中获取的异步函数以及 Windows 10 上的环境变量。我已在 GCS 中创建了一个存储桶并上传了我的 .raw 文件。

然后我创建了一个 .env 文件,其中包含以下内容

HOST=本地主机

端口=3000

GOOGLE_APPLICATION_CREDENTIALS=GDeveloperKey.json

在 AWS Lambda 中执行此操作只需将代码包装在 exports.handler = async (event, context,callback) => {

中即可

如何在 Windows 10 中本地模拟相同的内容?

// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
// const gcsUri = 'gs://my-bucket/audio.raw';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';

const config = {
  encoding: encoding,
  sampleRateHertz: sampleRateHertz,
  languageCode: languageCode,
};

const audio = {
  uri: gcsUri,
};

const request = {
  config: config,
  audio: audio,
};

// Detects speech in the audio file. This creates a recognition job that you
// can wait for now, or get its result later.
const [operation] = await client.longRunningRecognize(request);
// Get a Promise representation of the final result of the job
const [response] = await operation.promise();
const transcription = response.results
  .map(result => result.alternatives[0].transcript)

最佳答案

await 语句包装到立即调用的异步函数中。

例如:

(async () => {
  // Detects speech in the audio file. This creates a recognition job that you
  // can wait for now, or get its result later.
  const [operation] = await client.longRunningRecognize(request);
  // Get a Promise representation of the final result of the job
  const [response] = await operation.promise();
  const transcription = response.results
    .map(result => result.alternatives[0].transcript)
})();

关于javascript - 在 Windows 10 中使用 NodeJS 在本地运行异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57559019/

相关文章:

javascript 验证 zip 正则表达式

javascript - jquery ajax POST方法不执行函数

mysql - Ubuntu Apache 服务器中的 HDD 空间即将耗尽

google-cloud-platform - 如何利用多个 Google Cloud TPU 训练单个模型

javascript - 关注前面的输入时显示下一个 div,然后再次隐藏它(CSS3、JavaScript、Jquery)

javascript - 将 HTML5 canvas 扩展到整个文档

javascript - 使用 Express.js/Node.js 的电子邮件管道

javascript - 简单的游戏循环 Socket.io + Node.js + Express

node.js - 无法使用本地主机 URL 和 MongoJS 查询 MongoDB

python - 如何从浏览器访问在 Google Compute Engine 虚拟机上运行的 Django?