Skip to content

Commit 4b4dc80

Browse files
committed
Node
1 parent 362f372 commit 4b4dc80

File tree

8 files changed

+123
-0
lines changed

8 files changed

+123
-0
lines changed

Advance Nodejs/1. nextTick.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function hideString(str, done) {
2+
process.nextTick(()=>{
3+
done(str);
4+
});
5+
}
6+
7+
hideString("hello world", (hidden)=>{
8+
console.log(hidden);
9+
})
10+
11+
console.log('end');

Advance Nodejs/2. delay.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function delay(seconds, callback) {
2+
setTimeout(callback, seconds*1000);
3+
}
4+
5+
delay(2, () => {
6+
console.log('two seconds');
7+
delay(1, () => {
8+
console.log('three seconds');
9+
delay(1, () => {
10+
console.log('four seconds');
11+
});
12+
})
13+
})

Advance Nodejs/3. Promise.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var delay = (seconds) => new Promise((resolves, rejects)=> {
2+
setTimeout(resolves('the long delay has ended'), seconds*1000);
3+
});
4+
5+
delay(1).then(console.log)
6+
.then(()=> 42)
7+
.then((number)=> console.log(`hello world - ${number}`))
8+
9+
console.log('end first tick');

Advance Nodejs/4. Promise Error.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var delay = (seconds) => new Promise((resolves, rejects)=> {
2+
throw new Error('argh');
3+
setTimeout(resolves('the long delay has ended'), seconds*1000);
4+
});
5+
6+
delay(1).then(console.log)
7+
.then(()=> 42)
8+
.then((number)=> console.log(`hello world - ${number}`))
9+
.catch((error)=>console.log(`Error: ${error.message}`))
10+
11+
console.log('end first tick');

Advance Nodejs/5. Promise Rejects.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var delay = (seconds) => new Promise((resolves, rejects)=> {
2+
if(seconds > 3) {
3+
rejects(new Error(`${seconds} is too long`));
4+
}
5+
throw new Error('argh');
6+
setTimeout(resolves('the long delay has ended'), seconds*1000);
7+
});
8+
9+
delay(4).then(console.log)
10+
.then(()=> 42)
11+
.then((number)=> console.log(`hello world - ${number}`))
12+
.catch((error)=>console.log(`Error: ${error.message}`))
13+
14+
console.log('end first tick');

Advance Nodejs/6. promisify.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var { promisify } = require('util');
2+
3+
var delay = (seconds, callback) => {
4+
if(seconds > 3) {
5+
callback(new Error('${seconds} seconds it too long!'));
6+
}
7+
else {
8+
setTimeout(()=> callback(null, `the ${seconds} seconds delay is over`),
9+
seconds
10+
);
11+
}
12+
}
13+
14+
var promisifyDelay = promisify(delay);
15+
16+
promisifyDelay(5)
17+
.then(console.log)
18+
.catch((error) => console.log(`error: ${error.message}`));

Advance Nodejs/7. Promise.race.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var fs = require('fs');
2+
var { promisify } = require('util');
3+
var writeFile = promisify(fs.writeFile);
4+
var unlink = promisify(fs.unlink);
5+
var readdir = promisify(fs.readdir);
6+
var beep = () => process.stdout.write("\x07");
7+
var delay = (seconds) => new Promise((resolves) => {
8+
setTimeout(resolves, seconds*1000);
9+
})
10+
11+
Promise.race([
12+
delay(5),
13+
delay(2),
14+
delay(3),
15+
delay(5)
16+
]).then(() => readdir(__dirname))
17+
.then(console.log);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var fs = require('fs');
2+
var { promisify } = require('util');
3+
var writeFile = promisify(fs.writeFile);
4+
var unlink = promisify(fs.unlink);
5+
var beep = () => process.stdout.write("\x07");
6+
var delay = (seconds) => new Promise((resolves) => {
7+
setTimeout(resolves, seconds*1000);
8+
})
9+
10+
const doStuffSequentially = () => Promise.resolve()
11+
.then(() => console.log('starting'))
12+
.then(() => delay(1))
13+
.then(() => 'waiting')
14+
.then(console.log)
15+
.then(() => delay(2))
16+
.then(() => writeFile('file.txt', 'Sample File...'))
17+
.then(beep)
18+
.then(() => 'file.txt created')
19+
.then(console.log)
20+
.then(() => delay(3))
21+
.then(() => unlink('file.txt'))
22+
.then(beep)
23+
.then(() => 'file.txt removed')
24+
.then(console.log)
25+
.catch(console.error);
26+
27+
doStuffSequentially()
28+
.then(() => console.log('again again!!!'))
29+
.then(() => doStuffSequentially())
30+
.then(() => console.log('enough already...'));

0 commit comments

Comments
 (0)