我正在努力运行从 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/