【问题标题】:How do async for, removing element after processing处理后如何异步处理,删除元素
【发布时间】:2019-11-14 09:44:44
【问题描述】:

例如... 我想迭代所有月份的日子并删除月份等年份......

const max = 1000;
const min = 1000;

var years = [2019, 2018, 2017, 2016, 2015];
var months = [1,2,3,4,5,6,7,8,9,10,11,12];
var days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];

function delay(time) {
  return new Promise(function(resolve) { 
    setTimeout(resolve, time);
  });
}

(async () => {
    await delay(Math.floor(Math.random() * max) + min);
    for await (const year of years) {

        await delay(Math.floor(Math.random() * max) + min);
        for await (const month of months) {

            await delay(Math.floor(Math.random() * max) + min);
            for await (const day of days) {

                await console.log('Years:\n  ' + years);
                await console.log('Months:\n  ' + months);
                await console.log('Days:\n  ' + days);

                await console.log('= = = = = = = Removing day: ' + day);
                var index = days.indexOf(day);
                if (index !== -1) days.splice(index, 1);
            }
            days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];

            await console.log('= = = = = = = Removing month: ' + month);
            var index = months.indexOf(month);
            if (index !== -1) months.splice(index, 1);
        }
        months = [1,2,3,4,5,6,7,8,9,10,11,12];

        await console.log('= = = = = = = Removing year: ' + year);
        var index = years.indexOf(year);
        if (index !== -1) years.splice(index, 1);
    }

})();

我预计程序会迭代当天的数组,月份的数组,最后是年份的数组,但它返回一个混乱的输出......

【问题讨论】:

  • 您不应该操作您正在迭代的数组。这会导致混乱。但是既然你总是删除第一个元素,为什么不改用while (years.length > 0) { ... years.shift(); }呢?
  • 旁注:您错误地使用了for await (const x of y)for-await-of 循环用于循环异步迭代器。数组提供同步迭代器。你只需要for (const x of y)
  • 你为什么要从数组中删除东西?好像没必要……
  • 非常感谢 Thomas 和 T.J.克劳德,我正在删除项目以表明该项目已被处理,我有一些异步进程,当我遇到错误时,我需要再次运行,除了已处理的项目。我将使用 while 代替。

标签: javascript arrays node.js asynchronous async-await


【解决方案1】:

我将使用 use while 代替,谢谢!

const max = 1000;
const min = 1000;

var years = [2019, 2018, 2017, 2016, 2015];
var months = [1,2,3,4,5,6,7,8,9,10,11,12];
var days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];

function delay(time) {
  return new Promise(function(resolve) { 
    setTimeout(resolve, time);
  });
}

(async () => {
    await delay(Math.floor(Math.random() * max) + min);
    while (years.length > 0) {

        await delay(Math.floor(Math.random() * max) + min);
        while (months.length > 0) {

            await delay(Math.floor(Math.random() * max) + min);
            while (days.length > 0) {

                await console.log('Years:\n  ' + years);
                await console.log('Months:\n  ' + months);
                await console.log('Days:\n  ' + days);

                await console.log('= = = = = = = Removing day: ' + days[0]);
                await days.shift();
            }
            days = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];

            await console.log('= = = = = = = Removing month: ' + months[0]);
            await months.shift();
        }
        months = [1,2,3,4,5,6,7,8,9,10,11,12];

        await console.log('= = = = = = = Removing year: ' + years[0]);
        await months.shift();
    }

})();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多