-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_assignment_19.js
More file actions
150 lines (133 loc) · 2.59 KB
/
js_assignment_19.js
File metadata and controls
150 lines (133 loc) · 2.59 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
PROMISES ASSIGNMENT
1).
function job() {
return new Promise(function(resolve, reject) {
reject();
});
}
let promise = job();
promise
.then(function() {
console.log('Success 1');
})
.then(function() {
console.log('Success 2');
})
.then(function() {
console.log('Success 3');
})
.catch(function() {
console.log('Error 1');
})
.then(function() {
console.log('Success 4');
});
What is the output of the code above ?
A=The output of the code above will be:
Error 1
Success 4
2). Write a sleep function using a promise in javascript?
A=function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
console.log('Starting...');
await sleep(2000);
console.log('Two seconds have passed!');
3)What is the output of the following code?
const promise = new Promise(res => res(2));
promise.then(v => {
console.log(v);
return v * 2;
})
.then(v => {
console.log(v);
return v * 2;
})
.finally(v => {
console.log(v);
return v * 2;
})
.then(v => {
console.log(v);
});
A=The output of the following code will be:
2
4
undefined
8
4).
console.log('start')
const fn = () => (new Promise((resolve, reject) => {
console.log(1);
resolve('success')
}))
console.log('middle')
fn().then(res => {
console.log(res)
})
console.log('end')
What is the output of this code snippet?
A=The output of this code snippet will be:
start
middle
1
end
success
5). Write a function delay that returns a promise. And that promise
should return a setTimeout that calls resolve after 1000ms.
A=function delay(ms) {
return new Promise(function(resolve) {
setTimeout(resolve, ms);
});
}
console.log('start');
delay(1000).then(function() {
console.log('after 1000ms');
});
console.log('end');
6).
Promise.resolve('Success!')
.then(data => {
return data.toUpperCase()
})
.then(data => {
console.log(data)
})
What will the output be?
A=The output of this code will be SUCCESS!.
7).
var p = new Promise((resolve, reject) => {
reject(Error('The Fails!'))
})
.catch(error => console.log(error))
.then(error => console.log(error))
What will the output be?
A=The output will be an error message "Error: The Fails!"
8).
console.log('start')
setTimeout(() => {
console.log('setTimeout')
})
Promise.resolve().then(() => {
console.log('resolve')
})
console.log('end')
What will the output be?
A=start
end
resolve
setTimeout
10).
console.log('start')
Promise.resolve(1).then((res) => {
console.log(res)
})
Promise.resolve(2).then((res) => {
console.log(res)
})
console.log('end')
What will the output be?
A=start
end
1
2