Skip to content

Instantly share code, notes, and snippets.

View anshuman-singh-93's full-sized avatar
🏠
Working from home

Anshuman Singh anshuman-singh-93

🏠
Working from home
View GitHub Profile
// user.controller.js
const { UserError } = require('./util')
const saveUser = async ({ body } = {}) => {
if (!body.name) {
throw new UserError('name field is missing',400); // operational error
}
let result1 = await saveUserInDB(body);
// user.route.js
const express = require('express');
const router = express.Router();
const { saveUser } = require('./user.controller');
const { Helper } = require('./util');
router.post('/', Helper.requestHandler.call(null,saveUser));
// exporting router object to main file where we have app object
module.exports = router;
// user.route.js
const express = require('express');
const router = express.Router();
const { saveUser } = require('./user.controller');
router.post('/', saveUser);
// exporting router object to main file where we have app object
module.exports = router;
class UserError extends Error {
constructor(message, statusCode) {
super(message)
this.name = this.constructor.name;
this.statusCode = statusCode;
Error.captureStackTrace(this, UserError)
}
}
@anshuman-singh-93
anshuman-singh-93 / user.controller.js
Last active January 12, 2020 06:22
user.controller.js
const saveUser = async (req, res) => {
let { body } = req;
let result1, result2;
if (typeof body !== 'object') {
return res.status(400).json({ message: 'data must be an object' })
}
try {
result1 = await saveUserInDB(body);
}
const promisifyIt = (fn) => (...args)=>{
return new Promise((resolve,reject)=>{
fn(...args,(err,data)=>{
if(err){
reject(err)
}
else{
resolve(data)
}
})
0x45bef9Be3B4B43e674a2A8A0E9375867A1718CC7
0xC01af13d073E6eb9c2e4B6CBC789F3f02f9F9CD6
@anshuman-singh-93
anshuman-singh-93 / generate-pushid.js
Created June 28, 2017 16:48 — forked from mikelehen/generate-pushid.js
JavaScript code for generating Firebase Push IDs
/**
* Fancy ID generator that creates 20-character string identifiers with the following properties:
*
* 1. They're based on timestamp so that they sort *after* any existing ids.
* 2. They contain 72-bits of random data after the timestamp so that IDs won't collide with other clients' IDs.
* 3. They sort *lexicographically* (so the timestamp is converted to characters that will sort properly).
* 4. They're monotonically increasing. Even if you generate more than one in the same timestamp, the
* latter ones will sort after the former ones. We do this by using the previous random bits
* but "incrementing" them by 1 (only in the case of a timestamp collision).
*/
@anshuman-singh-93
anshuman-singh-93 / latency.txt
Created March 25, 2017 18:55 — forked from jboner/latency.txt
Latency Numbers Every Programmer Should Know
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns 3 us
Send 1K bytes over 1 Gbps network 10,000 ns 10 us
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD