Promise in JavaScript

Example :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>JavaScript</title>
</head>
<body>
    <script>
        //promise example
        let promiseToCleanTheRoom = new Promise(function (resolve,reject){
            //clean the room code here
            let clean = false;
            if(clean){
                resolve('Clean');
            }else{
                reject('Not Clean');
            }
        });
        promiseToCleanTheRoom.then(function(fromResolve){
            console.log('The room is : '+fromResolve);
        }).catch(function(fromReject){
            console.log('The room is '+fromReject);
        });
    </script>
</body>
</html>

Leave a Comment