【问题标题】:JavaScript Function ReEntrant in promise object承诺对象中的 JavaScript 函数可重入
【发布时间】:2018-02-21 15:37:47
【问题描述】:

我想在 promise 对象中重入函数。

此函数包含异步处理。 但是,此功能不起作用。 要指定,不触发,下一个“then 方法”。

代码在这里

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();
        }
    });
});
}

我把这个函数叫做Added Array and Promise.All()。

提前致谢。

【问题讨论】:

  • if 分支中,你永远不会解决承诺......
  • 避免使用Promise constructor antipattern,并且永远不要使用成功/错误回调,而只能使用可链接的then 回调!
  • 如果你在 promise.all 内部调用,如果一个失败,它会失败其他人,这就是你没有得到结果的原因。

标签: javascript promise


【解决方案1】:

你可以试试递归函数。将结果存储在一个数组中(不是全局的,而是将其传递给递归函数)。每个结果集都存储 guid,因此您知道什么结果集来自哪个 guid(当请求开始失败时,您知道到目前为止您做了什么)。

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);
    }  
  }
)

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 2019-07-23
    • 1970-01-01
    • 2020-07-05
    • 2016-11-11
    • 2017-12-18
    • 2021-03-25
    • 2018-09-07
    • 2015-08-13
    相关资源
    最近更新 更多