Azure-Samples / cognitive-services-speech-sdk

Sample code for the Microsoft Cognitive Services Speech SDK
MIT License
2.88k stars 1.85k forks source link

Unable to access variables inside callback of SpeakerIdentification & SpeakerVerification #1126

Closed manju605 closed 3 years ago

manju605 commented 3 years ago

I have implemented Speaker Independent-Identification feature in node js, below is the code snippet of it. but we get identification response as a callback function, in that callback function i am unable to access variable outside of that call-back(result) function.

What is solution?

await client.createProfileAsync( sdk.VoiceProfileType.TextIndependentIdentification, locale, async (result) => { var profile = result; if (profile) { //use global variable } else { //use global variable }

}
glharper commented 3 years ago

@manju605 In the code you've listed, "var profile" is declared in the scope of the callback. What happens with the example below?

Also, is this for westus region? Currently Speaker Identification and Verificaition features are in preview, and only available for westus, as documented here

let profile = undefined;
await client.createProfileAsync(
 sdk.VoiceProfileType.TextIndependentIdentification,
 locale,
 async (result) => {
 profile = result;
 if (!!profile) {
  console.log("profile created with id:" + profile.profileId); 
  //use global variable
 }
 else {
  console.error("no result returned!");
  //use global variable
 }
}