0

I thought I understood promises until this happened. I know there are ALOT of similar questions on SO but after going through many many and redoing promises this simple problem is not getting solved. I have been reading and trying but cannot find out how to fix this without using a callback function and not using a .then

const searchResponse = await performSearch(options);
if (searchResponse.error) {
    response.error = searchResponse.error;
}
else {
    response = orchestrateShareClassSearchResponse(searchResponse['data']);
}
return Promise.resolve(response);

The problem is searchResponse['data'] is always sent as an empty object and the result from performSearch ( async function performSearch) is not populated. I also tried await searchResponse['data'] but to no avail.

Sometimes a something tiny can really bug. Does anyone have any ideas besides using callbacks ?

I am posting the performSearch as well although didn't wan't to complicate the problem:

export async function performSearch(options, callingMethod: string, callback?: Function): Promise<IActionResponse<ISearchEntityResponse>> {
    const baseSearchRequest = Object.assign({}, BASE_SEARCH_REQUEST, options);
    let response = <IActionResponse<ISearchEntityResponse>>{};
    let searchResponse;// = await performSearch(options);
        const io = require('socket.io-client');
        const socket = io.connect();
        socket.on('connect', function (data) {
            console.log('CONNECTED TO CLIENT SOCKET');

            socket.emit('fetchShareClasses', null);
            socket.on('fetchShareClasses', async function (searchResponse) {
                console.log('sNullOrUndefined(searchResponse)::', isNullOrUndefined(searchResponse));
                if (searchResponse === null) {

                    searchResponse = await fetch(ENDPOINTS.fund.searchEntity, generateRequest('POST', baseSearchRequest)).then(function (searchResponse) {

                        console.log('NULL recieved do call to get DATA');
                        socket.emit('fetchShareClasses', searchResponse);
                    });
                }
                else {
                    console.log('fetchShareClasses CLIENT REACHED');
                    response = searchResponse;

                }
            });
        });
        return Promise.resolve(response);
}
Afshin Ghazi
  • 2,784
  • 4
  • 23
  • 37

0 Answers0