forked from johnandblue/async-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.js
32 lines (29 loc) · 828 Bytes
/
callbacks.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
'use strict';
const fs = require('fs');
exports.uploadFolder = (source, cb) => {
fs.readdir(source, function (err, files) {
if (err) {
cb('Error finding files: ' + err)
} else {
const fileQueue = [];
files.forEach(function (filename, fileIndex) {
const filePath = source+'/'+filename
// Uploading the file
fileQueue.push(filePath)
fs.readFile(filePath, function(err, data){
if (err) {
console.log('Error reading file: ' + err)
} else {
// Simulating file upload
setTimeout(() => {
fileQueue.splice(fileQueue.indexOf(filePath), 1);
if(fileQueue.length === 0) {
cb(null, files.length)
}
}, 1000)
}
})
})
}
})
}