forked from Zelechos/Pragmatic_JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise45.js
More file actions
46 lines (38 loc) · 1.65 KB
/
Copy pathExercise45.js
File metadata and controls
46 lines (38 loc) · 1.65 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
// 45) con Promesas realizar una validacion de un array para luego invertir ese array
'use strict'
const invertArray = (array)=>{
if(!(array instanceof Array))
return Promise.reject(`error 999 : the value is [${typeof array}] was expected => Array`);
return new Promise((resolve, reject)=>{
console.warn(`cargando...`);
let time = 3000;
setTimeout(() => {
// Creamos un objetos para enviar el array y su inversa
const arraysContainer = {
array,
arrayReverse : array.map(element => element).reverse(),
}
resolve(arraysContainer);
}, time);
});
}
const array = [2,3,4,5,9,8,7];
// aqui le aplicamos recusividad a la promesa
invertArray(array)
.then(arrayObject => {
console.log(`the array is : ${arrayObject.array} , the reverse of array is : ${arrayObject.arrayReverse}`);
return invertArray(["ether", "net", "etherias", "zelechos"]);
})
.then(arrayObject => {
console.log(`the array is : ${arrayObject.array} , the reverse of array is : ${arrayObject.arrayReverse}`);
return invertArray(['(', '{', '(', ')', '}', ')']);
})
.then(arrayObject =>{
console.log(`the array is : ${arrayObject.array} , the reverse of array is : ${arrayObject.arrayReverse}`);
return invertArray(12354);
})
.then(arrayObject => {
console.log(`the array is : ${arrayObject.array} , the reverse of array is : ${arrayObject.arrayReverse}`);
return invertArray({username:"etherias", password:"hacker"});
})
.catch(error => console.error(error));