Skip to content

Instantly share code, notes, and snippets.

@implicit-invocation
Created April 13, 2018 10:26
Show Gist options
  • Save implicit-invocation/b0f18348134e72ae4bd1d077d3920512 to your computer and use it in GitHub Desktop.
Save implicit-invocation/b0f18348134e72ae4bd1d077d3920512 to your computer and use it in GitHub Desktop.
// const express = require('express');
// const http = require('http');
// const app = express();
// const items = ['Di cho', 'Rua bat', 'Nau com'];
// const handleError = func => (req, res, next) => {
// try {
// func(req, res, next);
// } catch (e) {
// next(e);
// }
// };
// const todoRouter = express.Router();
// todoRouter.get('/', (req, res, next) => {
// res.send(items);
// });
// todoRouter.get('/:index', (req, res, next) => {
// const index = parseInt(req.params.index);
// if (!items[index]) {
// const error = new Error('Todo item not found');
// error.status = 404;
// throw error;
// }
// res.send(items[index]);
// });
// todoRouter.get('/test/test', (req, res) => {
// let i = 0;
// const sendNext = () => {
// setTimeout(() => {
// res.write(`${i}`);
// i++;
// if (i < 10) {
// sendNext();
// } else {
// res.end();
// }
// }, 1000);
// };
// sendNext();
// });
// app.use((req, res, next) => {
// console.log(`${new Date().toString()} - ${req.method} - ${req.originalUrl}`);
// next();
// });
// app.get('/divide/:a/:b', (req, res) => {
// const a = parseFloat(req.params.a);
// const b = parseFloat(req.params.b);
// if (b === 0) {
// throw new Error('Cannot divided by 0');
// }
// res.send(`${a} / ${b} = ${a / b}`);
// });
// app.get('/', (req, res) => res.send('Hello!'));
// app.use('/todos', todoRouter);
// app.use((err, req, res, next) => {
// if (err) {
// res.status(err.status || 500);
// res.send(`${err.status || 500} - ${err.message}`);
// } else {
// next();
// }
// });
// http.createServer(app).listen(3000, () => console.log(`Server started.`));
// const mongoose = require('mongoose');
// mongoose.connect('mongodb://localhost/test-async');
// const Item = mongoose.model(
// 'item',
// {
// title: String
// },
// 'items'
// );
const items = [];
const save = (title, cb) => {
setTimeout(() => {
const item = {
title,
id: items.length
};
items.push(item);
cb(null, item);
}, Math.random() * 100);
};
const names = ['di cho', 'rua bat', 'nau com'];
const forEach = (arr, func, done) => {
let completed = 0;
let results = [];
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
func(item, (err, result) => {
results[i] = result;
completed++;
if (completed >= arr.length) {
done(null, results);
}
});
}
};
const forEachSeries = (arr, func, done) => {
let results = [];
const runCurrent = current => {
if (current >= arr.length) {
done(null, results);
return;
}
let item = arr[current];
func(item, (err, result) => {
if (err) {
done(err);
return;
}
results[current] = result;
runCurrent(current + 1);
});
};
runCurrent(0);
};
forEachSeries(
names,
(name, cb) => {
save(name, (err, item) => {
console.log(item);
cb(err, item);
});
},
(err, items) => console.log(items)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment