Unable to process the response from a AJAX request inside the "createdRow" function

Unable to process the response from a AJAX request inside the "createdRow" function

abhartiyaabhartiya Posts: 5Questions: 3Answers: 0

Hello,

While using the createdRow function, I am not able to process the response from an AJAX request. Is there any way I can set a timeout and only carry on the createdRow functionality if I have some response?

My datatable initialization looks like this:

$('#dataTables-example').DataTable( {
                                "bProcessing": false,
                                "destroy": true,
                                "aaData": data,
                                "sDefaultContent": "",
                                "aoColumns": [
                                    {"mData": "test"},
                                    {
                                         "mRender": function (data, type, full)
                                        {
                                             btn = '<div id="progress' + crow + '"></div>'
                                            crow++;
                                            return btn;
                                        }
                                    }
                        ],
                                "createdRow": function( row, data, index ) {
                                    var currow = index;
                                    var info = data;
                                    issuelink = start_long_task(info);
                                        }
                    } );

I am getting the data value above in the Datatable from a previously run AJAX query.

So, basically the Datatable after getting populated with the data, I am going through each row that was created and trying to call the function start_long_task(info) where info is that row data.

The start_long_task(info) is a function where I am sending an AJAX request to my Celery-Redis backend so it goes and sits in a queue there. This function looks like this:

function start_long_task(info) {

                 $.ajax({
                type: 'POST',
                url: '/retrievejiraticket',
                contentType: 'application/json',
                data: JSON.stringify(info),
                processData: false,
                dataType: 'json',
                success: function(data, status, request){
                         status_url = request.getResponseHeader('Location');
                         issuelink = update_progress(status_url);
                         if (typeof issuelink === 'undefined'){
                             setTimeout(function(){ update_progress(status_url); }, 10000);
                             issuelink = update_progress(status_url);
                            }
                          return issuelink;
                     },
                error: function() {
                    alert('Unexpected error');
                }
             });

        }

As you can see, this calls the function update_progress(status_url) to retrieve the issuelink of the task that we submitted. That function looks like this:

function update_progress(status_url) {
 $.getJSON(status_url, function(data) {
                if (data['state'] != 'PENDING' && data['state'] != 'PROGRESS') {
                    if ('issue_link' in data) {
                        return data['issue_link'];
                    }
                    else {
                        return data['state'];
                    }
                }
                else {
                    setTimeout(function() { update_progress(status_url); }, 2000);
                }
            });
        }

So, the problem that I am facing is that when I call the createdRow function above, the value of issuelink initially is undefined since we just start the Celery task that time. But, then I haven't been able to figure out any way to update this value by timing out and waiting for the actual issuelink value once the Celery task finishes.

What are my options to solve this problem?

Thanks

This discussion has been closed.