0

I'm saving list items using promise in a loop and it works just fine. When all items are saved I want to execute some code using .done(function()). But how? My code below executes "done" directly before any items are beeing saved.

$.when(reader.onload = function (e) 
{
  var rows = e.target.result.split("\n");
  for (var i = 0; i < rows.length; i++) 
  {
    if(rows[i])
    {
      addCompanyItem(rows[i]); // store as list item in SharePoint
    }
  } 
}).done(function () 
{
  alert("all items are saved"); // execute when done, not working
})

Thanks for reply @JussiPalo but promises.push gives me a syntax error?

reader.onload = function (e) 
{
  var promises = [];

  for (var i = 0; i < rows.length; i++) 
  {
    if(rows[i])
    {
      promises.push(addCompanyItem(rows[i])); // <-- syntax error
    }
  } 

  $.when.apply($, promises).then(function() {
    alert("All set!");
  };
}

function addCompanyItem(companyRow) 
{
    var d = $.Deferred();    

    var clientContext = SP.ClientContext.get_current();
    var companyList = clientContext.get_web().get_lists().getByTitle('CompanyList');
    var companyItem = companyList.addItem(new SP.ListItemCreationInformation());  

    [...]

    companyItem.set_item('Title', companyData[0]);
        var obj = {d:d, companyData:companyData};

        companyItem.update();

    clientContext.load(companyItem);

    clientContext.executeQueryAsync(
        Function.createDelegate(obj, successCallback), 
        Function.createDelegate(obj, failCallback)
    );

    return d.promise();
}

function successCallback() 
{
    this.d.resolve(this.companyData);
}

closed as off-topic by Robert Lindgren, SPArchaeologist-様, Gaurravs, Mohamed El-Qassas MVP Dec 14 '17 at 17:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to SharePoint are off-topic here, but can be asked on Stack Overflow." – Robert Lindgren, SPArchaeologist-様, Gaurravs, Mohamed El-Qassas MVP
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $.when() expects you to pass it one or more jQuery promises or deferreds. It does not know what to do when you pass it a function. – jfriend00 Dec 14 '17 at 14:14
2

Not entirely sure how you initialize the deferred function calls, but $.when.apply is the key here.

var promises = [];

var rows = e.target.result.split("\n");
  for (var i = 0; i < rows.length; i++) 
  {
    if(rows[i])
    {
      promises.push(addCompanyItem(rows[i])); // store as list item in SharePoint
    }
  } 

$.when.apply($, promises).then(function() {
    // return values are in arguments array, i.e., arguments[N] 

};


function addCompanyItem(item) {
    var dfd = $.Deferred();

    // some ajax call to SP, when it returns, resolve dfd
    spAjaxCall().done(function(data) { dfd.resolve(data);})

    return dfd.promise();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.