-
Notifications
You must be signed in to change notification settings - Fork 3
/
37.1 promises example.html
49 lines (46 loc) · 1.63 KB
/
37.1 promises example.html
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//here i have four simple promises and they always resolved, they are never rejected
const video1=new Promise((resolve,reject)=>{
resolve("video one recorded")
})
const video2=new Promise((resolve,reject)=>{
resolve("video two recorded")
})
const video3=new Promise((resolve,reject)=>{
resolve("video three recorded")
})
const video4=new Promise((resolve,reject)=>{
resolve("video four recorded")
})
//now here i want to run all these three prmoises in paralled same time
//promise.all will run all of these promises and when its done its going to call .then and .catch
Promise.all([ // takes array of promises to run them
video1,
video2,
video3,
video4
]).then((messages)=>{ //.then contains array of all the successfull message
console.log(messages) // also try console.log(messages[0])
})
//no catch is required here as they all return resolve
//promise.race will give us only one promises which is completed first
Promise.race([
video4,
video1,
video2,
video3
]).then((message)=>{
console.log(message) // video 4 recorded as we mention video 4 here first
})
</script>
</body>
</html>