Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1981d30

Browse files
committed
Adding error first and promise examples
1 parent d68e256 commit 1981d30

File tree

6 files changed

+52
-0
lines changed

6 files changed

+52
-0
lines changed

error-first-example.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const fs = require('fs');
2+
const location = __dirname + '/sometext.txt';
3+
4+
fs.readFile(location, 'utf-8', processFile);
5+
6+
function processFile(err, result) {
7+
if (err) {
8+
console.error(err);
9+
}
10+
console.log(result);
11+
}

error-first-lambda.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const fs = require('fs');
2+
const location = __dirname + '/howoldisJaxNode.txt';
3+
4+
fs.readFile(location, 'utf-8', (err, results) => {
5+
if (err) {
6+
console.error(err);
7+
}
8+
console.log(results);
9+
});

howoldisJaxNode.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The first JaxNode User Group met on October 16th, 2013.

promisify-example.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const fs = require('fs');
2+
const util = require('util');
3+
const location = __dirname + '/sometext.txt';
4+
5+
const readFileAsync = util.promisify(fs.readFile);
6+
7+
readFileAsync(location, 'utf-8').then(data => {
8+
console.log(data);
9+
}).catch(err => {
10+
console.error(err);
11+
});

sometext.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Happy Birthday 5th JaxNode User Group!!!

turn-errfirstcb-into-promise.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fs = require('fs');
2+
const location = __dirname + '/sometext.txt';
3+
4+
readFilePromise(location).then(result => {
5+
console.log(result);
6+
}).catch(err => {
7+
console.error(err);
8+
})
9+
10+
function readFilePromise(path) {
11+
return new Promise(function (resolve, reject) {
12+
fs.readFile(path, 'utf-8', (err, result) => {
13+
if (err) {
14+
reject(err);
15+
}
16+
resolve(result);
17+
})
18+
});
19+
}

0 commit comments

Comments
 (0)