JS Async: 13. Promise: get product

by istiq20

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Promise: get product</title>
</head>
<body>

<table>
    <tbody>
    <tr>
        <td>Id :</td>
        <td id="id"></td>
    </tr>
    <tr>
        <td>Name :</td>
        <td id="name"></td>
    </tr>
    <tr>
        <td>Price :</td>
        <td id="price"></td>
    </tr>
    </tbody>
</table>

<script>

    function getProduct(product) {
        return new Promise(function (resolve, reject) {

            const ajax = new XMLHttpRequest();
            ajax.open("GET", `api/${product}.json`);
            ajax.onload = function () {
                if (ajax.status === 200) {
                    resolve(ajax.responseText);
                } else {
                    reject(`Error Get Product with response : ${ajax.status}`);
                }
            };
            ajax.send();

        });
    }

    getProduct("product1");
    getProduct("product2");
    getProduct("product3");

</script>

</body>
</html>

JavaScript

// file: product1.json
{
  "id": "1",
  "name": "MacBook Pro 15",
  "price": 30000000
}

// file: product2.json
{
  "id": "2",
  "name": "MacBook Pro 15",
  "price": 30000000
}

// file: product3.json
{
  "id": "3",
  "name": "MacBook Pro 15",
  "price": 30000000
}