Home > Software engineering >  use aysnc in angular
use aysnc in angular

Time:02-15

I have this save_data method which is being called on button click

save_recording(){ 
let formData=new FormData();
formData.append("id",id); 
const transcript_arr = [];
const confidence_arr = [];
const service_transcript=this.ServiceTranscriptData();
const service_confidence=this.ServiceConfidenceData();
  for (var i = 0; i < service_confidence.length; i  ) {
  }
..
..
..
}

  async ServiceTranscriptData()
    {  
      var trans = await this.service.getTranscriptValue();
      return trans;
    }
    async ServiceConfidenceData()
    {  
      var confidence = await this.service.getConfidenceValue();
      return confidence;
    }

i want till this.ServiceTranscriptData() & this.ServiceConfidenceData() is not executed, rest of the code following these lines of code should not get executed. That's why i use async.

Getting error Property length doesn't exist on type Promise <any[]> enter image description here enter image description here

Any solution to resolve this issue. Thanks

Service :

import { Injectable } from '@angular/core';
declare var webkitSpeechRecognition: any;


@Injectable({
  providedIn: 'root'
})
export class VoiceRecognitionService {

  
  isListening = false;
  public text = '';
  public tempWords : any;
  public transcript_arr =[];
  public confidence_arr =[];
  public temp_trans ='';
  myWindow = window as any;
  recognition;
  //console.log(recognition);

  constructor(private toastr: ToastrService) {
    //console.log(this.recognition);
   
    //if(this.recognition)
    const SpeechRecognition = this.myWindow.SpeechRecognition || this.myWindow.webkitSpeechRecognition;

    if (SpeechRecognition != null && SpeechRecognition !== undefined) {
    this.recognition =  new webkitSpeechRecognition(); 
    }
    else
    {
    this.recognition = null;
    this.toastr.error('Warning', 'Web Speech Not Supported on this browser');
    }

   }
  getTranscriptValue()
  {
    return this.transcript_arr;
  }
  getConfidenceValue()
  {
    return this.confidence_arr;
  }
  init() {
    if(this.recognition) {
    this.recognition.continuous = true;
    this.recognition.interimResults = false;
    this.recognition.maxAlternatives = 1;
    this.recognition.lang = 'en-US';
    this.startListening();
    }
 
  }
  startListening() {
    if(this.recognition) {
    this.recognition.addEventListener('result', (e:any) => {
      let last = e.results.length - 1;
      this.temp_trans = e.results[last][0].transcript; 
      let confidence = e.results[last][0].confidence; 
      this.confidence_arr.push(confidence);
      this.transcript_arr.push(this.temp_trans); 
    });
  }
  }

  start() {
    if(this.isListening==false)
    {
      this.isListening = true;
      try{
      this.recognition.start();
      }
      catch(e){

      }
      
    }
    if (this.recognition) {
    this.recognition.addEventListener('end', (condition:any) => {
      if (!this.isListening) {
        try{
       this.recognition.stop();
        }
        catch(e)
        {

        }

      } else {
        this.wordConcat();
        try{
        this.recognition.start();
        }
        catch(e)
        {

        }
      }
    });
  }
  }
  stopListening() {
    if (this.recognition) {
    this.recognition.removeEventListener('result',null);
    }
  }
  stop() {
    this.isListening = false;
    this.wordConcat();
    try{
    this.recognition.stop();
    }
    catch(e)
    {

    }
  }
  reinit()
  {  
    this.transcript_arr=[];
    this.confidence_arr=[];
    this.tempWords='';
    this.text='';
    this.temp_trans='';
  }
  wordConcat() {
    this.text = this.text   ' '   this.tempWords   '.';
    this.tempWords = '';
  }
}

CodePudding user response:

  async save_recording() {
    const formData = new FormData();
    formData.append("id",id); 
    const transcript_arr = [];
    const confidence_arr = [];
    const service_transcript = await this.service.getTranscriptValue();
    const service_confidence = await this.service.getConfidenceValue();
    for (let i = 0; i < service_confidence.length; i  ) {}
  }

CodePudding user response:

because this two methods also returns promise ServiceTranscriptData() ServiceConfidenceData() and you need to await for them too

async save_recording(){ 
 ...
 const service_transcript= await this.ServiceTranscriptData();
 const service_confidence=await this.ServiceConfidenceData();
 for (var i = 0; i < service_confidence.length; i  ) {
  }

}
  • Related