Skip to content

Instantly share code, notes, and snippets.

View Abazhenov's full-sized avatar

Alexei Bazhenov Abazhenov

View GitHub Profile
const getInfo = () =>
axios.get('/users')
.then(users => {
console.log(users)
})
.then(() => getGroups())
.then(groups => {
console.log(groups)
})
.then(() => getFavorites())
const getInfo = async () => {
console.log(await axios.get('/users'))
console.log(await getGroups())
console.log(await getFavorites())
return 'all done';
}
getInfo();
const associateUsers = () => {
try {
doSynchronousThings()
return getUsers()
.then(users => users.map(user => user.getAddress()));
.catch(e => console.error(e))
} catch(err){
console.error(err)
}
}
const associateUsers = async () => {
try {
doSynchronousthings()
const users = await getUsers()
return users.map(user => user.getAddress());
} catch(err){
console.error(err)
}
}
router.get('/user/:id', async (req, res, next) => {
try {
const user = await getUserFromDb({ id: req.params.id })
res.json(user);
} catch (e) {
//this will eventually be handled by your error handling middleware
next(e)
}
})
const asyncMiddleware = fn =>
(req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};
const asyncMiddleware = require('./utils/asyncMiddleware');
router.get('/users/:id', asyncMiddleware(async (req, res, next) => {
/*
if there is an error thrown in getUserFromDb, asyncMiddleware
will pass it to next() and express will handle the error;
*/
const user = await getUserFromDb({ id: req.params.id })
res.json(user);
}));
const db = require('../_db');
const Sequelize = require('sequelize');
class tag extends Sequelize.Model {
//methods, hooks, etc....
}
tag.init({
name: {
type: Sequelize.STRING(100),
const db = require('../_db');
const Sequelize = require('sequelize');
class story extends Sequelize.Model {
//methods, hooks, etc....
}
story.init({
title: {
type: Sequelize.STRING,
@Abazhenov
Abazhenov / index.js
Last active September 26, 2017 05:02
const Tag = require('./tag')
const Story = require('./story')
Tag.belongsToMany(Story)
Story.belongsToMany(Tag)