In my Type Script code, I am looking to store the return value of a function in a local variable. The process is outlined below:
getdetail1(store){
let Cust_id=this.sharedata.latus_lead.m_type
let url="http:domain.com"
console.log(url);
let res;
this.loginservice.leaddata = null;
this.loginservice.getdetails(url).then(data=>{
let lead=data;
this.details=lead[0];
res=lead[0];
});
return res;
}
To call the function, follow these steps:
let res = this.getdetail1(store);
The following snippet showcases the login service code utilized:
getdetails(query){
if (this.leaddata) {
// already loaded data
console.log("already data loaded lead") ;
return Promise.resolve(this.leaddata);
}
// don't have the data yet
return new Promise(resolve => {
// We're using Angular HTTP provider to request the data,
// then on the response, it'll map the JSON data to a parsed JS object.
// Next, we process the data and resolve the promise with the new data.
this.http.get(query)
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.leaddata = data;
resolve(this.leaddata);
});
});
}
This section covers how promises are handled within the code.