Как сделать асинхронное удаление элемента после обработки - PullRequest
0 голосов
/ 03 июля 2019

Только для примера ... Я хочу перебрать все месячные дни и удалить месяц и т. Д. С годом ...

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

})();

Я ожидал, что программа итерирует массив дня, месяца и, наконец, год, но возвращает результат вывода ...

1 Ответ

0 голосов
/ 03 июля 2019

Я буду использовать вместо этого, спасибо!

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

})();
...