sharepoint online – Add JSPDF as a list item attachment

We are trying to save an attachment to multiple list items created concurrently. The PDF files are created dynamically using jspdf and stored against the list items

Below are the functions that create list item attachment by calling a common function named addItem().

Functions that call addItem()

function p1()
{
   CreateItem(fromEmail,distEmail,ccEmail,subject,body).then(function(resp){
                        
   console.log(resp);
   var Lid = resp.d.Id; // gets the list item ID
   var PDFStream = PDFArray; // Global Variable to get the jsPDF File
   var Fname = DFileName; // Global Variable to get the name of the attachment
   addItem(Lid,PDFStream,Fname);
  });
}

function p2()
{
   CreateItem(fromEmail,distEmail,ccEmail,subject,body).then(function(resp){
                        
   console.log(resp);
   var Lid = resp.d.Id; // gets the list item ID
   var PDFStream = PDFArray; // Global Variable to get the jsPDF File
   var Fname = DFileName; // Global Variable to get the name of the attachment
   addItem(Lid,PDFStream,Fname);
  });
}

Function to add attachment

function addItem(Lid,PDFFile,Fname) {
                        
            var File = PDFFile;
            var deferred = $.Deferred();           

                $.ajax({
                    url: _spPageContextInfo.webAbsoluteUrl +
                     "/_api/web/lists/getbytitle('ExtEmailsList')/items(" + Lid + ")/AttachmentFiles/add(FileName='"+Fname+"')",
                    type: 'POST',
                    data: File,
                    processData: false,
                    headers: {
                        "Accept": "application/json; odata=verbose",
                        "content-type": "application/json; odata=verbose",
                        "X-RequestDigest": $('#__REQUESTDIGEST').val(),
                        "content-length": File.length
                    },
                    success: function (data) {          
                    deferred.resolve(data);
                    },
        
                    error: function (err) {
                    deferred.reject(err);
                    }
               }); // ajax | POST      
        return deferred.promise();    
      };

The above function is called multiple times , However the function intermittently creates the attachment and the other times it fails and the attachment column is blank. Say function 1 calls addItem , it creates the attachment however the call from function 2 fails and vice versa.

I tried debugging , However there are no errors. Would appreciate if anyone could let us know how we can get this to work with multiple functions calling addItem().

Thanks in advance