-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
137 lines (121 loc) · 3.32 KB
/
Copy pathapp.js
File metadata and controls
137 lines (121 loc) · 3.32 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
// console.log(a);
// var a = 10;
// var a = 100;
// var a = 1000;
// console.log(a);
//keyword var defines variable common in all parts of JS
function f(a) {
console.log(a, this.a);
}
//let b = 20;
// setTimeout(f.bind({a:"Hello"}, b), 1000);
// f.call({a: "World"}, b)
//f.bind({a:"Hello"}, b);
//setTimeout(() => f.call({a: "World"}, b), 1000)
// function getId() {
// return new Promise(resolve => {
// setTimeout(()=>resolve(100), 1000);
// })
// }
// function getUserName() {
// return new Promise(resolve => {
// setTimeout(() => resolve("user123"), 500);
// })
// }
//1. scenario - we should get id and only after that we get username
// async function f1() {
// const id = await getId();
// console.log(id);
// const username = await getUserName();
// console.log(username)
// }
// f1()
// getId().then(id =>{ console.log(id)
// return getUserName()}).then(username => console.log(username))
//2. scenario simultanious performing getId and getUserName
// const promiseId = getId();
// const promiseUser = getUserName();
// Promise.all([promiseId, promiseUser])
// .then(idUser => console.log(`id: ${idUser[0]}, username: ${idUser[1]} `))
//
//3. scenario there will be considered only one result from promise moving in resolved state first
// const promiseId = getId();
// const promiseUser = getUserName();
// Promise.race([promiseId, promiseUser]).then(res => console.log(res, typeof res))
// for (var i = 0; i < 3; i++) {
// setTimeout(() =>{
// console.log(i)
// }, 1);
// }
// for (let i = 0; i < 3; i++) {
// setTimeout(() =>{
// console.log(i)
// }, 1);
// }
// function getAge() {
// "use strict";
// age = 21;
// console.log(age);
// }
// getAge();
// let person = { name: "Lydia" };
// const members = [person];
// person = null;
// console.log(members);
// const a = {};
// const b = { key: "b" , toString: function() {return "b"}};
// const c = { key: "c" };
// a[b] = 123;
// a[c] = 456;
// console.log(a[b]);
//let a = 2;
// function greeting() {
// throw "Hello world!";
// }
// function sayHi() {
// const data = greeting();
// console.log("It worked!", data);
// }
// sayHi();
// function sum(num1, num2=40) {
// console.log(num1 + num2)
// }
// sum(10)
// async function getData() {
// return 'I made it!';
// }
// (async () => {const data = await getData()/*.then(data => console.log(data))*/;
// console.log(data)})()
// const myPromise = () => Promise.resolve('I have resolved!');
// async function firstFunction() {
// console.log(await myPromise(), 100);
// console.log('second 101');
// }
// async function secondFunction() {
// console.log(await myPromise(), 105);
// console.log('second 106');
// console.log('second 107');
// }
// firstFunction();
// secondFunction();
//HW #28
class Deferred {
#functions
constructor() {
this.#functions = [];
}
then(fun) {
this.#functions.push(fun);
}
resolve(value) {
this.#functions.forEach(fun => value = fun(value))
}
}
const d = new Deferred()
d.then(function(res){ console.log("1 ", res); return "a"; });
d.then(function(res){ console.log("2 ", res); return "b"; });
d.then(function(res){ console.log("3 ", res); return "c"; });
d.resolve('hello');
// 1 hello
// 2 a
// 3 b