Skip to content

Instantly share code, notes, and snippets.

@flavioespinoza
Forked from Atinux/async-foreach.js
Last active October 24, 2018 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flavioespinoza/b7b0ed6e40b11437ad9d65fd130213fe to your computer and use it in GitHub Desktop.
Save flavioespinoza/b7b0ed6e40b11437ad9d65fd130213fe to your computer and use it in GitHub Desktop.
JavaScript: async/await with forEach()
const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
const asyncForEach = (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
const start = async () => {
await asyncForEach([1, 2, 3], async (num) => {
await waitFor(50)
console.log(num)
})
console.log('Done')
}
start()
const log = require('ololog').configure({locate: false})
const _ = require('lodash')
const Chance = require('chance')
const chance = new Chance()
const _error = function (method, err, socket) {
log.lightYellow(`${method}__ERROR `, err.message)
if (socket) {
socket.emit(`${method}__ERROR `, err.message)
}
}
const bcrypt = require('bcrypt')
const db = require('./mongo-db')
const BCRYPT_SALT_ROUNDS = 12
const salt_rounds = 12
function hash_string (string) {
return new Promise(function (resolve) {
bcrypt.hash(string, BCRYPT_SALT_ROUNDS)
.then(function (hashed_string) {
resolve(hashed_string)
})
})
}
async function validate_user (username, password) {
db.get_one(db.users_cl, {_id: username})
.then(async function (user_obj) {
const match = await bcrypt.compare(password, user_obj.password)
if (match) {
//login
}
})
}
async function map_api_keys (obj) {
try {
const api_key = await bcrypt.hash(obj.api_key, salt_rounds)
const secret = await bcrypt.hash(obj.secret, salt_rounds)
return {
exchange_name: obj.exchange_name,
api_key: api_key,
secret: secret
}
} catch (err) {
_error('map_api_keys', err)
}
}
async function __loop (array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
async function hash_exchange (api_keys) {
try {
let result = []
await __loop(api_keys, async (obj) => {
let hashed_keys = await map_api_keys(obj)
log.cyan(hashed_keys)
result.push(hashed_keys)
})
log.lightYellow('result', result)
return result
} catch (err) {
_error('hashed_exchange', err)
}
}
async function register (req, res, next) {
try {
const username = req.body.username
const hashed_password = await bcrypt.hash(req.body.password, salt_rounds)
const email = req.body.email
const hashed_email = await bcrypt.hash(email, salt_rounds)
const api_keys = [
{
exchange_name: 'hitbtc',
api_key: '536a94129a1d159409db05e73e259fc1',
secret: '5c2259a5aab8fa0e505d2a1818843dff'
},
{
exchange_name: 'bittrex',
api_key: 'e8d2650f49254c599d4f8aca375cfa93',
secret: 'db808cb30da14c79b967b566cb1ec56c'
}
]
const hashed_exchange = await hash_exchange(api_keys)
log.blue('hashed_exchange', hashed_exchange)
const user_hash = {
_id: username,
username: username,
call_sign: username,
password: hashed_password,
exchange: hashed_exchange,
email: hashed_email
}
const user = {
_id: username,
username: username,
call_sign: username,
email: email
}
log.lightYellow('user_hash', JSON.stringify(user_hash, null, 2))
db.update(db.users_cl, user_hash)
.then(function () {
res.render('register', {user: user})
})
} catch (err) {
_error('register', err)
}
}
module.exports = {register}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment