As i understand your question, you need to handle the response data outside of given function. In this case you can simply return the pnp func call, so it returns a Promise:
function GetServiceListData():Promise<any>
{
return pnp.sp.web.lists.getByTitle('Test').items
.select('Title,FullName,DOJ,ManagerName/Title')
.expand('ManagerName')
.get();
}
function successCallback(response){
//do things with response in a callback
}
function errorCallback(error){
//handle error
}
//usage
GetServiceListData()
.then(successCallback)
.catch(errorCallback);
If you want to do some preprocessing with the response data first, you have two options.
First you can go with async function to flattern out your function for readability
async function GetServiceListData():Promise<any>
{
let preprocessedData;
let response;
try {
response = await pnp.sp.web.lists.getByTitle('Test').items
.select('Title,FullName,DOJ,ManagerName/Title')
.expand('ManagerName')
.get();
//do things with response in place
} catch(error) {
//handle error
}
//do things with response in place
return preprocessedData;
}
Or you can still use the Thenable format. In this case you need to return the preprocessed data inside the .then
function GetServiceListData():Promise<any>
{
return pnp.sp.web.lists.getByTitle('Test').items
.select('Title,FullName,DOJ,ManagerName/Title')
.expand('ManagerName')
.get()
.then(response => {
let preprocessedData;
//do things with response
return preprocessedData;
});
}
Important to notice, that in all cases, you are returning a Promise
, so you can use the .then
and .catch
methods to do stuff with the data outside GetServiceListData
.
Promise
Async function