Я думаю, если вы заверните сокет в обещание и разрешите результат, он должен работать:
export function performSearchTemp(options: ISearchEntityRequest, callingMethod?: string): Promise<IActionResponse<any>> {
const baseSearchRequest = Object.assign({}, BASE_SEARCH_REQUEST, options);
let response = <IActionResponse<ISearchEntityResponse>>{};
const io = require('socket.io-client');
const socket = io.connect();
return new Promise( (resolve) => {
socket.on('connect', async function (data) {
socket.emit('fetchShareClasses', 'init');
socket.on('fetchShareClasses', async function (searchResponse) {
if (searchResponse === 'init') {
searchResponse = await fetch(ENDPOINTS.fund.searchEntity, generateRequest('POST', baseSearchRequest))
.then(function (a) {
return a.json(); // call the json method on the response to get JSON
})
.then(function (json) {
socket.emit('fetchShareClasses', JSON.stringify(json));
});
resolve(searchResponse);
}
else {
console.log("test searchResponse ORCHESTRATE ::");
searchResponse =
JSON.stringify(searchResponse);
**//THIS HAS THE CORRECT DATA**
response = JSON.parse(searchResponse);
console.log("response", response)
resolve(response);
}
});
});
});
}