Skip to content
Advertisement

is there any workaround for returning the value of res from readCsv() method instead of undefined?

the scenario is i am having the file uploader, which accept .json,.csv file after having file, If i clicked on upload button ,then homeComponent call the service dataparser , which is having two function readJson readCsv, and readcsv
function returning the observable other one returns array ,this is simple i have to call the both function
on if else and handle the subscriber and array on HomeComponent But wait i do not want to handle these complexities at component level so i want to return only array of data on the home component page so, I have handle these on service but observable creating problem in this

**dataparser.service.ts having threee methods**

JavaScript

readJson method is returning array of objects and m is not empty if type of file is json its returning m to caller function on homeComponent

JavaScript

readCsv method is returning empty varaiable(res) due to subscribe method is there any workaround through which i could get valve of res from readCsv function and i do not want subscribe this method anywhere because i do not want handle these two result on homeComponent page, so then how can i get the value of this res varaible from readCsv

JavaScript

Advertisement

Answer

You cannot await Observables directly, however, you can await a Promise. You can simply use the .toPromise() method on the observables subscription. Consider the following: const date = await dateSubscription.toPromise();

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement