Загрузка данных


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Работа Promises</title>

    <style>
        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            padding: 20px;
            font-family: Arial, sans-serif;
            background-color: white;
        }

        h1 {
            text-align: center;
        }

        .content {
            display: flex;
            gap: 20px;
        }

        .explanation {
            width: 100%;
            background-color: rgb(166, 169, 169);
            padding: 20px;
            border-radius: 4px;
        }

        .states {
            display: flex;
            gap: 20px;
            text-align: center;
            margin-top: 20px;
        }

        .pending,
        .fulfilled,
        .rejected {
            flex: 1;
            padding: 20px;
            border: 2px solid #555;
            border-radius: 10px;
        }

        .pending {
            background-color: #f7ef7f;
        }

        .fulfilled {
            background-color: lightgreen;
        }

        .rejected {
            background-color: lightcoral;
        }

        .code-block {
            margin-top: 25px;
            min-height: 300px;

            padding: 25px;

            background-color: #252525;
            color: white;

            border-radius: 10px;

            font-family: monospace;
            white-space: pre-wrap;
        }

        .demo-area {
            margin-top: 20px;
        }

        .demo-area h2 {
            margin-bottom: 10px;
        }

        button {
            padding: 10px 20px;
            margin-right: 10px;

            font-size: 16px;

            border: 1px solid #555;
            border-radius: 5px;

            cursor: pointer;
        }

        .console {
            margin-top: 20px;
            min-height: 100px;

            background-color: #252525;
            color: white;

            padding: 15px;
            border-radius: 5px;

            font-family: monospace;
        }

        .log-entry {
            margin: 5px 0;
        }

        .info {
            color: yellow;
        }

        .success {
            color: lightgreen;
        }

        .error {
            color: lightcoral;
        }
    </style>
</head>


<body>

    <h1>Promise в JS</h1>


    <div class="content">

        <div class="explanation">

            <h2>Что такое Promise?</h2>

            <p>
                Promise (с англ. «обещание») в JavaScript -
                это специальный объект, который представляет
                результат асинхронной операции.
            </p>


            <div class="states">

                <div class="pending">
                    <h3>Pending</h3>
                    <p>Ожидание</p>
                </div>

                <div class="fulfilled">
                    <h3>Fulfilled</h3>
                    <p>Исполнено</p>
                </div>

                <div class="rejected">
                    <h3>Rejected</h3>
                    <p>Отклонено</p>
                </div>

            </div>

        </div>

    </div>


    <div class="code-block">const promise = new Promise((resolve, reject) => {

    setTimeout(() => {

        const success = Math.random() > 0.5;

        if (success) {
            resolve("Promise выполнен успешно");
        } else {
            reject("Promise завершился ошибкой");
        }

    }, 2000);

});</div>


    <div class="demo-area">

        <h2>Демо работы Promise</h2>

        <p>Нажми на кнопку</p>

        <button id="createpromise">
            Создать Promise
        </button>

        <button id="clearconsole">
            Очистить консоль
        </button>

        <div id="console" class="console"></div>

    </div>


    <!-- тут начинается JS -->

    <script>

        const consoleBlock = document.getElementById('console');


        function addLog(message, type = 'info') {

            const logEntry = document.createElement('div');

            logEntry.className = `log-entry ${type}`;

            logEntry.textContent = `> ${message}`;

            consoleBlock.appendChild(logEntry);

        }


        const createPromiseButton =
            document.getElementById('createpromise');

        const clearConsoleButton =
            document.getElementById('clearconsole');


        createPromiseButton.addEventListener('click', function () {

            addLog('Promise создан');

            addLog('Состояние: Pending');


            const promise = new Promise((resolve, reject) => {

                setTimeout(() => {

                    const success = Math.random() > 0.5;


                    if (success) {

                        resolve('Promise выполнен успешно');

                    } else {

                        reject('Promise завершился ошибкой');

                    }

                }, 2000);

            });


            promise
                .then(function (result) {

                    addLog('Состояние: Fulfilled', 'success');

                    addLog(result, 'success');

                })

                .catch(function (error) {

                    addLog('Состояние: Rejected', 'error');

                    addLog(error, 'error');

                });

        });


        clearConsoleButton.addEventListener('click', function () {

            consoleBlock.innerHTML = '';

        });

    </script>

</body>

</html>