Skip to content

Instantly share code, notes, and snippets.

View saiumesh535's full-sized avatar
🤒
so long

sai umesh saiumesh535

🤒
so long
View GitHub Profile
@saiumesh535
saiumesh535 / idea.js
Created December 2, 2017 14:39
asyncAwaitIdea
async function loginController(){
try{
const a = await loginService().
catch((error)=> {throw new CustomErrorHandler({code: 101, message:"a failed",error: error})});
const b = await someUtil().
catch((error)=> {throw new CustomErrorHandler({code: 102, message:"b failed",error: error})});
//someoeeoe
if(a && b) console.log("no one failed")
}catch(error){
@saiumesh535
saiumesh535 / app.js
Created November 13, 2017 07:50
Writing custom emitter's in nodejs
// let's code some emitters
function Emitter(){
this.events = {};
}
const emtr = new Emitter();
// on function to push events in array
Emitter.prototype.on = function(type,listener){
this.events[type] = this.events[type] || [];
@saiumesh535
saiumesh535 / app.js
Last active November 8, 2017 11:09
Creating JWT
var express = require('express');
var app = express();
const expressjwt = require('express-jwt')
const jsonwebtoken = require('jsonwebtoken');
// this will verify the token before going to actual requested route (middleware)
// except the "/login" route every request needs token in header which will look like "Authorization:Bearer eyJh..........." refer image in story
app.use(expressjwt({secret: "some key"}).unless({path: ['/', '/login']}))
@saiumesh535
saiumesh535 / asyncawaitwithouttrycatch.js
Last active February 19, 2020 18:23
async await without try catch
async function getData(){
const a = await someFunction().catch((error)=>console.log(error));
const b = await someOtherFunction().catch((error)=>console.log(error));
if(a && b ) console.log("some result")
}
@saiumesh535
saiumesh535 / asyncawait.js
Last active November 3, 2017 06:38
Asyncawait
async function check(req, res) {
try {
const a = await someOtherFunction();
const b = await somethingElseFunction();
res.send("result")
} catch (error) {
res.send(error.stack);
}
}
async function check(req, res) {
someOtherFunction().then((a) => {
somethingElseFunction().then((b) => {
res.status(200).json({a: a, b: b});
}).catch((error) => {
res.send("error");
})
}).catch((error) => {
res.send("error");
})
@saiumesh535
saiumesh535 / somefile.js
Last active February 8, 2018 23:01
Async await without try catch
// traditional way of writing code
const router = require('express').Router();
router.get('/check', check);
module.exports = router;
async function check(req, res) {
someOtherFunction().then((a) => {
somethingElseFunction().then((b) => {
res.status(200).json({a: a, b: b});
}).catch((error) => {
@saiumesh535
saiumesh535 / HttpCaller.java
Created August 28, 2017 11:43
Volley Server request class
/**
* this class helps us to make http calls
*/
public class HttpCaller {
private Context context;
//private ProgressDialog pDialog;
private HttpProgressDialog httpProgressDialog;
@saiumesh535
saiumesh535 / jsonwebtokenexample.js
Last active August 11, 2017 06:12
creating and verifying "jsonwebtoken" in express as middleware.
// source : https://www.npmjs.com/package/jsonwebtoken
// check token information at : https://jwt.io/
const jwt = require('jsonwebtoken');
// secret key
const secretKey = 'some secret key';
// this is for verifying and creating token middleware
app.use((req, res, next) => {
// if the requested URL is login then create token otherwise verify
@saiumesh535
saiumesh535 / newPromise.js
Last active August 21, 2017 18:18
Async Await is new form writing in JavaScript/Node 8
// for examples goto https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
// this function will return a promise
function multiplication(a, b) {
return new Promise((resolve, reject) => {
resolve(a * b)
})
}
// whn you write async before function it allows you to return promise which was being returned from other function