【问题标题】:Promise.all does not wait till .then()Promise.all 不会等到 .then()
【发布时间】:2018-10-11 15:06:50
【问题描述】:

我在使用 Promise.all 时遇到问题。如果我理解正确 - someArrayUpdate1 异步解析几个对数组 (someArray1) 的承诺 - 所以当 Promise.all(someArray1) 命中时,它迟早会拥有所有数据。 someArrayUpdate2 也会发生同样的事情 - 所以它开始立即 构建它的数据 (someArray2)。

我认为 .then() 会像刹车一样工作,所以 Promise.all(someArray2) 将开始构建它的承诺数组 AFTER Promise.all(someArray1) 并感谢 TEST1 (mainArray) 对象状态将打开。

现在的问题是 TEST1 (mainArray) 对象状态为 OFF。我不知道如何改变它。有什么想法吗?

<doctype>
<html>
<head>
    <title>TEST</title>
</head>
<body>

<img src="img1.jpg" />
<img src="img2.jpg" />
<img src="img3.jpg" />

<script>

    // ARRAYS and FUNCTIONS:
    var mainArray = [{
        TEST1 : {
            status : ''
        },
        TEST2 : {
            status : ''
        },
        TEST3 : {
            status : ''
        }
    }];

    var someArray1 = []; // for imgs
    var someArray2 = []; // for objects

    function someArray1Update(imgSrc, imgCounter) {
        return new Promise(function (resolve, reject) {
            setTimeout(function() {// - for marking asynchronous data flow

                    // this is where the problem is (lets assume that this code must be placed here) - we update second array here for Promise.all(someArray2) but nothing happen... status is OFF
                    someArray2.push(someArray2Update('TEST1', 'on'));

                    return resolve({counter : imgCounter, url : imgSrc});
            }, 1000);
        });
    }

    function someArray2Update(object, status) {
        return new Promise(function(resolve, reject) {
            return resolve({
                [object] : {
                    status : status,
                }
            });
        });
    }

    // CODE:
    someArray2.push(someArray2Update('TEST2', 'on')); // working fine

    var imgs = document.images || document.getElementsByTagName('img');
    var imgsLength = imgs.length;
    var imgSrc = '';
    var imgsInfo = '';

    if (imgsLength !== 0) {
        for (var i = 0; i < imgsLength; i++) {
            imgSrc = imgs[i].getAttribute('src');

            var imgCounter = i + 1;

            someArray1.push(someArray1Update(imgSrc, imgCounter));

            console.log('Image ' + [i + 1] + ' - ' +  imgSrc);
            imgsInfo  += 'Image ' + [i + 1] + ' - ' +  imgSrc + '<br />';
        }
    }
    else;

    someArray2.push(someArray2Update('TEST3', 'on')); // working fine

    Promise.all(someArray1).then(function(all) {
        console.log('\n');
        all.forEach(function(i) {
            console.log('someArray1 - ok');
        });
        console.log('\n');
    }).then(Promise.all(someArray2).then(function(array) {
            array.forEach(function(i) {
                mainArray[0] = Object.assign(mainArray[0], i);
            });
            console.log('\n=========================================================');
            console.log(mainArray);
            console.log('=========================================================\n');
        }));

</script>


</body>
</html>

【问题讨论】:

  • 你必须将回调函数传递给then,而不是Promise.all(someArray2).then(…)的结果
  • 老实说,我不知道它应该是什么样子(以异步方式与代码中的许多 someArray2Update 函数。你能举个例子吗?谢谢。
  • 你已经用.then(function(all) { … })做得很好了。
  • 哦,明白了...这是一个代码错误,感谢@Bergi 指出。

标签: asynchronous promise


【解决方案1】:
).then(Promise.all(someArray2).then(function(array) {
            array.forEach(function(i) {
                mainArray[0] = Object.assign(mainArray[0], i);
            });
            console.log('\n=========================================================');
            console.log(mainArray);
            console.log('=========================================================\n');
        }));

这应该看起来像:

).then(*function()*{Promise.all(someArray2).then(function(array) {
            array.forEach(function(i) {
                mainArray[0] = Object.assign(mainArray[0], i);
            });
            }console.log('\n=========================================================');
            console.log(mainArray);
            console.log('=========================================================\n');
        }));

感谢@Bergi 指出这一点。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-09
  • 2017-08-28
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 2021-03-21
相关资源
最近更新 更多