repl.it
@ulisesantana/

sandbox_node

Nodejs

No description

fork
loading
Files
  • index.js
index.js
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
async function calcTax(amount, balance, tax){
  return (amount/balance)*tax
}

(async ()=>{
const startTime = Date.now();
const taxes = await calcTax(100, 1000, 200);
console.log(`${Date.now() - startTime}ms`, taxes)
})()


function getPromise(){
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      Math.random() > 0.5 ? resolve(Math.random()) : reject(new Error('Dummy error'))
    }, 1000);
  })
}

const prom = [...new Array(10)].reduce((acc) => {
  console.log('Doing something', acc)
  try {
    return {
      err: [...acc.err],
      results: [...acc.results, getPromise()]
    }
  } catch (err) { 
    return {
      err: [...acc.err, err],
      results: [...acc.results]
    }
  } 
}, {err: [], results: []});

(async ()=> {

console.log(await prom)
})()
Fetching token
?