Promise递归在promise.all之后完成,应将其添加到promise.all数组中

时间:2019-10-01 22:22:30

标签: javascript promise

我对promise递归有问题-它在promise.all完成后完成。并且应将其添加到promise.all数组,然后promise.all应该运行并完成。这是代码。我添加注释是为了更好地理解问题的代码。

var imgsUrl_a = ['https://instagram.fpoz2-1.fna.fbcdn.net/vp/2f0a20dfc67e64e1909de7e1b0fac96b/5E2313AC/t51.2885-15/e35/29401170_172261436759303_4938764043058937856_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=111', 'https://instagram.fpoz2-1.fna.fbcdn.net/vp/640e2ff84467286ce9180cea0146de87/5E2DC599/t51.2885-15/e35/37865648_843225369200399_61004628845658112_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=111', 'https://instagram.fpoz2-1.fna.fbcdn.net/vp/3d389ede443d7126efda6b2eb48d5f0f/5E250685/t51.2885-15/e35/18723348_1439870969393169_1318300495944613888_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=109', 'https://instagram.fpoz2-1.fna.fbcdn.net/vp/399eecb59f7278f6961bfd46c6ea6b2c/5E37AF08/t51.2885-15/e35/20067230_502691503412565_7207018400441171968_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=109'];
    var imgsSize = 0;
    var imgsRequests = 0;
    var addition = 'https://instagram.fpoz2-1.fna.fbcdn.net/vp/fcd6c3e76c6d9aebeb394916f194dbb7/5E28B2B5/t51.2885-15/e35/67791124_2533908973355416_4649868652501841030_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=104';

    var imagesPath_a_pAll = [];
    var imagesSize_a_pAll = [];

    function imageSize_f_pAll(object, size) {
      return new Promise(function(resolve, reject) {
        if (((object === '') || (object === undefined) || (object === null)) || ((size === '') || (size === undefined) || (size === null)))
        {return reject(console.log('ERROR'));}
        else
        {return resolve({[object]: size});} // this goes to 'imagesSize_a_pAll' array
      }).catch(function(err)
      {console.error('ERROR - imageSize_f_pAll');
      });
    }

    function imagePath_f_pAll(img_get_attrib_src, img_i_counter) {
      return new Promise(function (resolve) {
        var dl = request(img_get_attrib_src, {method: 'HEAD'}, function (err, resp, img_body) {
          if (err || resp.statusCode >= 400) {
            console.log('ERROR');
            return resolve({counter : img_i_counter, url : img_get_attrib_src, size : 0, error : 'ERROR'});
          }
          if (!err && resp.statusCode == 200) {
            console.log('Image: ' + img_i_counter + ' address - ' + img_get_attrib_src);
            var img_headersContentLength = resp.headers['content-length'];
            if ((img_headersContentLength === undefined) || (img_headersContentLength === null) || img_headersContentLength === '') {
              img_headersContentLength = 0;
            }
            var img_headersContentLength_tonumber = + img_headersContentLength;
            imgsSize = imgsSize + img_headersContentLength_tonumber;

            if (img_i_counter === 4) // if we encounter LAST item od images array, we try to add one more image (addition)
            {
              console.log('additional image - ' + img_get_attrib_src);
              imagesPath_a_pAll.push(imagePath_f_pAll(addition, 5)); // this will perform AFTER promise.all, why?
            }

            imagesSize_a_pAll.push(imageSize_f_pAll('image-' + img_i_counter, imgsSize)); // if we use another promise array and function everything is working...
            return resolve({counter : img_i_counter, url : img_get_attrib_src, size : img_headersContentLength_tonumber}); // this goes to 'imagesPath_a_pAll' array
          }
        });
      });
    }


    if (imgsUrl_a.length !== 0) {
      for (var i = 0; i < imgsUrl_a.length; i++)
      {
        imgsRequests = imgsRequests + 1;
        imagesPath_a_pAll.push(imagePath_f_pAll(imgsUrl_a[i], i + 1));
      }
    }
    else {
      globals.pageSize_a_pAll.push(objectSize_f_pAll('images', 0));
      console.log('No images !?');
    }

    Promise.all(imagesPath_a_pAll).then(function(imagesPath_a_pAll) {
      console.log('=======================================');
      console.log('imagesPath_a_pAll:\n');
      console.log(imagesPath_a_pAll);
      // some code/calculations here
    }).then(function() {
      return Promise.all(imagesSize_a_pAll).then(function(imagesSize_a_pAll) {
        console.log('=======================================');
        console.log('imagesSize_a_pAll:\n');
        console.log(imagesSize_a_pAll);
        // some code/calculations here
      });
    })

您的帖子似乎主要是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。看起来您的帖子大部分是代码;请添加更多详细信息。

1 个答案:

答案 0 :(得分:0)

var imgsUrl_a = ['https://instagram.fpoz2-1.fna.fbcdn.net/vp/2f0a20dfc67e64e1909de7e1b0fac96b/5E2313AC/t51.2885-15/e35/29401170_172261436759303_4938764043058937856_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=111', 'https://instagram.fpoz2-1.fna.fbcdn.net/vp/640e2ff84467286ce9180cea0146de87/5E2DC599/t51.2885-15/e35/37865648_843225369200399_61004628845658112_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=111', 'https://instagram.fpoz2-1.fna.fbcdn.net/vp/3d389ede443d7126efda6b2eb48d5f0f/5E250685/t51.2885-15/e35/18723348_1439870969393169_1318300495944613888_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=109', 'https://instagram.fpoz2-1.fna.fbcdn.net/vp/399eecb59f7278f6961bfd46c6ea6b2c/5E37AF08/t51.2885-15/e35/20067230_502691503412565_7207018400441171968_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=109'];
var imgsSize = 0;
var imgsRequests = 0;
var addition = 'https://instagram.fpoz2-1.fna.fbcdn.net/vp/fcd6c3e76c6d9aebeb394916f194dbb7/5E28B2B5/t51.2885-15/e35/67791124_2533908973355416_4649868652501841030_n.jpg?_nc_ht=instagram.fpoz2-1.fna.fbcdn.net&_nc_cat=104';

var imagesPath_a_pAll = [];
var imagesSize_a_pAll = [];
var imagesAdditions_a_pAll = []; // new array for additional images

function imageSize_f_pAll(object, size) {
  return new Promise(function(resolve, reject) {
    if (((object === '') || (object === undefined) || (object === null)) || ((size === '') || (size === undefined) || (size === null)))
    {return reject(console.log('ERROR'));}
    else
    {return resolve({[object]: size});} // this goes to 'imagesSize_a_pAll' array
  }).catch(function(err)
  {console.error('ERROR - imageSize_f_pAll');
  });
}

function imagePath_f_pAll(img_get_attrib_src, img_i_counter) {
  return new Promise(function (resolve) {
    var dl = request(img_get_attrib_src, {method: 'HEAD'}, function (err, resp, img_body) {
      if (err || resp.statusCode >= 400) {
        console.log('ERROR');
        return resolve({counter : img_i_counter, url : img_get_attrib_src, size : 0, error : 'ERROR'});
      }
      if (!err && resp.statusCode == 200) {
        //console.log('Image: ' + img_i_counter + ' address - ' + img_get_attrib_src);
        var img_headersContentLength = resp.headers['content-length'];
        if ((img_headersContentLength === undefined) || (img_headersContentLength === null) || img_headersContentLength === '') {
          img_headersContentLength = 0;
        }
        var img_headersContentLength_tonumber = + img_headersContentLength;
        imgsSize = imgsSize + img_headersContentLength_tonumber;
        if (img_i_counter === 4) // if we encounter LAST item od images array, we try to add one more image (addition)
        {
          console.log('additional image - ' + img_get_attrib_src);
          //imagesPath_a_pAll.push(imagePath_f_pAll(addition, 5)); // this would perform AFTER promise.all - so it won't be added to it
          imagesAdditions_a_pAll.push(imagePath_f_pAll(addition, 5)); // we don't add it to "imagesPath_a_pAll" but to "imagesAdditions_a_pAll" that runs AFTER Promise.all(imagesPath_a_pAll) - problem is solved
        }
        imagesSize_a_pAll.push(imageSize_f_pAll('image-' + img_i_counter, imgsSize)); // again if we use another promise array and function everything is working cause stuff got its proper order
        return resolve({counter : img_i_counter, url : img_get_attrib_src, size : img_headersContentLength_tonumber}); // this goes to 'imagesPath_a_pAll' array
      }
    });
  });
}

if (imgsUrl_a.length !== 0) {
  for (var i = 0; i < imgsUrl_a.length; i++)
  {
    imgsRequests = imgsRequests + 1;
    imagesPath_a_pAll.push(imagePath_f_pAll(imgsUrl_a[i], i + 1));
  }
}

Promise.all(imagesPath_a_pAll).then(function(imagesPath_a_pAll)
{
    console.log('===================+++++++++++++++++=====================');
    console.log('imagesPath_a_pAll:\n');
    console.log(imagesPath_a_pAll);
}).then(function()
{
    return Promise.all(imagesAdditions_a_pAll).then(function(imagesAdditions_a_pAll)
    {
        console.log('===================+++++++++++++++++=====================');
        console.log('imagesAdditions_a_pAll:\n');
        console.log(imagesAdditions_a_pAll);
    }).then(function()
    {
        return Promise.all(imagesSize_a_pAll).then(function(imagesSize_a_pAll)
        {
            console.log('===================+++++++++++++++++=====================');
            console.log('imagesSize_a_pAll:\n');
            console.log(imagesSize_a_pAll);
        });
    });
});

贷方转到-Jaromanda X