Skip to content

Commit 97711be

Browse files
turn into promises
Signed-off-by: Arnav Gupta <[email protected]>
1 parent 848f719 commit 97711be

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

Lecture11/promises/callbacks.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function readFile1(path, done) {
2+
if (typeof path !== 'string' || path.length < 3) {
3+
done(new Error('Path invalid'))
4+
}
5+
setTimeout(() => {
6+
done(null, 'Sample data 1')
7+
}, 1000)
8+
}
9+
10+
function readFile2(path, done) {
11+
if (typeof path !== 'string' || path.length < 3) {
12+
done(new Error('Path invalid'))
13+
}
14+
setTimeout(() => {
15+
done(null, 'Sample data 2')
16+
}, 2000)
17+
}
18+
19+
function readFile3(path, done) {
20+
if (typeof path !== 'string' || path.length < 3) {
21+
done(new Error('Path invalid'))
22+
}
23+
setTimeout(() => {
24+
done(null, 'Sample data 3')
25+
}, 3000)
26+
}
27+
28+
function writeFile(data1, data2, data3, done) {
29+
setTimeout(() => {
30+
console.log('data written', [data1, data2, data3])
31+
done(null)
32+
}, 2000)
33+
}
34+
35+
readFile1('asdsad', (err, data1) => {
36+
readFile2('dnbdfgn', (err, data2) => {
37+
readFile3('dgngdn', (err, data3) => {
38+
writeFile(data1, data2, data3, () => {
39+
40+
})
41+
})
42+
})
43+
})

Lecture11/promises/promises.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
function readFile1(path) {
2+
return new Promise((resolve, reject) => {
3+
if (typeof path !== 'string' || path.length < 3) {
4+
reject(new Error('Path invalid'))
5+
}
6+
setTimeout(() => {
7+
resolve('Sample data 1')
8+
}, 1000)
9+
})
10+
}
11+
12+
function readFile2(path) {
13+
return new Promise((resolve, reject) => {
14+
if (typeof path !== 'string' || path.length < 3) {
15+
reject(new Error('Path invalid'))
16+
}
17+
setTimeout(() => {
18+
resolve('Sample data 2')
19+
}, 2000)
20+
})
21+
}
22+
23+
function readFile3(path) {
24+
return new Promise((resolve, reject) => {
25+
if (typeof path !== 'string' || path.length < 3) {
26+
reject(new Error('Path invalid'))
27+
}
28+
setTimeout(() => {
29+
resolve('Sample data 3')
30+
}, 3000)
31+
})
32+
}
33+
34+
function writeFile(data1, data2, data3, done) {
35+
return new Promise((resolve, reject) => {
36+
setTimeout(() => {
37+
console.log('data written', [data1, data2, data3])
38+
resolve()
39+
}, 2000)
40+
})
41+
}
42+
43+
readFile1('asdsad', (err, data1) => {
44+
readFile2('dnbdfgn', (err, data2) => {
45+
readFile3('dgngdn', (err, data3) => {
46+
writeFile(data1, data2, data3, () => {})
47+
})
48+
})
49+
})
50+
51+
let errH = (err) => {
52+
if (err) console.error(err)
53+
}
54+
55+
readFile1('asdasd')
56+
.then((data1) => {
57+
readFile2('')
58+
.then((data2) => {
59+
readFile3('dgngdn')
60+
.then((data3) => {
61+
writeFile(data1, data2, data3)
62+
})
63+
.catch(errH)
64+
})
65+
.catch(errH)
66+
})
67+
.catch(errH)

0 commit comments

Comments
 (0)