loading
main.js
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
31
32
33
34
function promiseRandomSeconds() { 
     let randomSeconds =  Math.floor((Math.random() * 10) + 1);
     return new Promise(
           function(resolve, reject) {
               // Perform logic and then determine to resolve or reject
               window.setTimeout(
                  function() {
                    if (randomSeconds%2 === 0) { 
                      // Resolve the promise if its even
                      resolve(randomSeconds + " seconds");
                    } else { 
                      // Reject the promise if its odd
                      reject(randomSeconds + " seconds");
                    }
               },randomSeconds*1000);
           }
     );
}
let test1 = promiseRandomSeconds();
let test2 = promiseRandomSeconds();

test1.then(function(result) {
	console.log("Success: " +result);
}).catch(function(error) { 
   console.log("Fail: " + error);
});

test2.then(function(result) {
	console.log("Success: " + result);
}).catch(function(error) { 
   console.log("Fail: " + error);
});

console.log("Hey there, I didn't have to wait!");
Native Browser JavaScript