0

I would like to reentrant function in promise object.

this function contains Asynchronous processing. however, this function dose NOT Work. To be specified, DOES NOT fired ,next "then method".

the code is here

loopcount = 0;

getItemcount = 0;
global_ItemCol = [];
function GetItem_in_List_Over5000(parentSiteUrl, listGuid) 

{

if (loopcount == 0) {
    console.log("Enter FirstTime");
    endPoint = parentSiteUrl + "/_api/Web/Lists(guid'" + listGuid + "')/Items?$top=3000&$select=Title,Id,ContentTypeId,HasUniqueRoleAssignments";
} else {
    console.log("Eneter SecondTime");
}

return new Promise(function (resolve_GetItem_in_List5000, reject_GetItem_in_List5000) {
    console.log("Eneter Inner Function");

    $.ajax({
        type: 'GET',
        url: endPoint,
        headers: { 'accept': 'application/json;odata=verbose', "X-RequestDigest": $("#__REQUESTDIGEST").val() },
        success: function (data) {
            console.log(data.d.__next);
            if (data.d.__next) {
                global_ItemCol = global_ItemCol.concat(data.d.results);
                endPoint = data.d.__next;
                loopcount++;
                console.log("looopcount increment. " + global_ItemCol.length);
                GetItem_in_List_Over5000(parentSiteUrl, listGuid);

            } else {
                global_ItemCol = global_ItemCol.concat(data.d.results);
                var local_col = [];
                local_col = local_col.concat(global_ItemCol);
                loopcount = 0;
                global_ItemCol.length = 0;
                resolve_GetItem_in_List5000(local_col);
                console.log("return call");
                //return Promise.resolve().then(local_col);
                resolve_GetItem_in_List5000(local_col);
            }

        },
        error: function (error) {
            OutputLog(error.responseJSON.error.message.value);
            loopcount = 0;
            reject_GetItem_in_List5000();
        }
    });
});
}

I called this function Added Array and Promise.All().

Thanks in advance.

Blue
  • 41
  • 5
  • Well in the `if` branch you never resolve the promise… – Bergi Feb 21 '18 at 05:25
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it), and never use success/error callbacks but chainable `then` callbacks only! – Bergi Feb 21 '18 at 05:26
  • If you call inside promise.all if one fails it'll fail others that's why you are not getting the result. – Rahul Sharma Feb 21 '18 at 05:56

1 Answers1

0

You could try a recursive function. Store results in an array (not global but pass it to the recursive function). With every result set store the guid so you know what result set came from what guid (when requests start failing you know what you've done so far).

function GetItem_in_List_Over5000(parentSiteUrl, listGuid) {
  const recur = (listGuid,results=[]) => 
    $.ajax({
      type: 'GET',
      url: parentSiteUrl + "/_api/Web/Lists(guid'" + listGuid + "')/Items?$top=3000&$select=Title,Id,ContentTypeId,HasUniqueRoleAssignments",
      headers: { 'accept': 'application/json;odata=verbose', "X-RequestDigest": $("#__REQUESTDIGEST").val() },
    }).then(
      function (data) {
        console.log(data.d.__next);
        if (data.d.__next) {
          return recur(
            data.d.__next,
            results.concat([listGuid,data.d.results])
          );
        } else {
          //add listGuid to result set so you know where it came from
          return results.concat([listGuid,data.d.results]);
        }

      }      
    ).fail(//newer jQuery can use .catch
      err=>({type:"error",error:err,results:results})
    );
  return recur(listGuid)
}

GetItem_in_List_Over5000("url","guid")
.then(
  results=>{
    if((results&&results.type)===error){
      console.log("something went wrong:",results.error);
      console.log("have some results:",results.results);
    }else{
      console.log("got all results:",results);
    }  
  }
)
HMR
  • 37,593
  • 24
  • 91
  • 160