-
Notifications
You must be signed in to change notification settings - Fork 13
/
async_functions.js
35 lines (34 loc) · 1.08 KB
/
async_functions.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
const axios = require('axios');
const Promise = require('bluebird');
const fs = require('fs');
const readFileAsync = Promise.promisify(require('fs').readFile);
module.exports = {
postData: (url, data) => axios(url, data),
getData: url => axios.get(url),
updateData: url => {},
deleteData: url => {},
readFile: file => {
return new Promise((resolve, reject) => {
fs.readFile(file, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
})
})
},
promisifiedReadFile: readFileAsync,
writeFile: (file, text) => {},
promisifiedWriteFile: Promise.promisify(require('fs').writeFile),
asyncTask1: (val1, val2, duration = 1500) => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(val1 + val2), duration)
})
},
asyncTask2: () => {},
asyncTask3: () => {},
asyncTask4: () => {},
asyncTask5: () => new Promise(resolve => setTimeout(() => resolve(console.log('2).')), 2500)),
asyncTask6: () => new Promise(resolve => setTimeout(() => resolve(console.log('2).')), 2000)),
}