Skip to content

Commit ec0f169

Browse files
author
DouglasHdezT
committed
add: Promises examples
1 parent 9336f9b commit ec0f169

21 files changed

Lines changed: 63 additions & 0 deletions

File tree

4-Promises/Example1.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Promises
3+
*/
4+
5+
const isMommyHappy = (daddyReturn = false) => {
6+
return new Promise((resolve, reject) => {
7+
if (daddyReturn) {
8+
resolve({
9+
isHappy: true,
10+
reason: "Papá regresó con los cigarros"
11+
});
12+
} else {
13+
reject(new Error("Nunca regresó :c"));
14+
}
15+
});
16+
}
17+
18+
isMommyHappy(true)
19+
.then(state => {
20+
console.log(state);
21+
})
22+
.catch(err => {
23+
console.log(err.message)
24+
});
25+
26+
isMommyHappy(false)
27+
.then(state => {
28+
console.log(state);
29+
})
30+
.catch(err => {
31+
console.log(err.message)
32+
});
33+
34+
isMommyHappy()
35+
.then(state => {
36+
console.log(state);
37+
})
38+
.catch(err => {
39+
console.log(err.message)
40+
});

4-Promises/Example2.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Promises to write a File
3+
*/
4+
5+
const fs = require("fs");
6+
7+
const appendInFile = text => {
8+
return new Promise((resolve, reject) => {
9+
try {
10+
fs.writeFileSync("./result.txt", text, { flag: "a" });
11+
resolve();
12+
} catch{
13+
reject(new Error("Falló al escribir en el archivo"));
14+
}
15+
});
16+
}
17+
18+
appendInFile("Mucho texto\n")
19+
.then(() => {
20+
console.log("¡Archivo escrito correctamente!")
21+
})
22+
.catch(err => {
23+
})

0 commit comments

Comments
 (0)