node js implementation of end function

var flag= ""
        if(request.method=='PUT')
        {
            request.on('end', function () 
            {  
                        var query = azure.TableQuery
                         if(!error){
                            for (var index in entities) {
                                    if(entities[index].RowKey==xxx)
                                    {                               
                                    flag=555;
                                    }
                                }
                            if (flag==="555")
                            {
                            response.writeHead(200, 
                            send success                                                                                                
                            }
                            else
                            {
                            console.log("the flag is ");
                            console.log(flag);
                            send failure
                            }
            });
        }

    }).listen(9200);

This is a Pseudo code. I want to get the 200 OK send based on the flg. But I see that always I receive 200 OK with data not present.

I kept some console logs and see that always:"the flag is " is printed first and than the console logs in if(entities[index].RowKey==xxx) are getting printed.

I feel from debug logs that azure query is taking time and by that tine node is trying to execute the rest of the code

I also want to know if I am missing something ?