Afshin Ghazi
Afshin Ghazi

Reputation: 2990

Returning promise while working with sockets

I am trying to get a value from sockets and then return this in the async function. I have read and tried and read some more on SO and elsewhere. Although there is data coming back from the "socket.on" and "response" is populated the value of this is not returned in the method and an empty object is returned.

Please do not mark as duplicate because I have tried many many threads about promises, asyn functions, socket, websockets etc etc here but have not been able to resolve.

   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();

            socket.on('connect', async function (data) {

                socket.on('fetchShareClasses', async function (searchResponse) {

                        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));
                            });
                    }

                    searchResponse = 
                    JSON.stringify(searchResponse);

                    **//THIS HAS THE CORRECT DATA**
                    response = JSON.parse(searchResponse);
                    console.log("​response", response)

                });
            });
            **//THIS RETURNS an EMPTY OBJECT TO THE CALLING METHOD**
            return Promise.resolve(response);

    }

Really appreciate any advice as I have spent alot of time solving this issue.

Upvotes: 1

Views: 2968

Answers (1)

Uma
Uma

Reputation: 846

I think if you wrap socket in promise and resolve the result, it should work:

 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);
                    }
                });
            });
        });
    }

Upvotes: 1

Related Questions