In the file users.js I want to get code
out of randomCode()
to assign to the result
and use it in whole endpoint '/login'
.
randomCode.js
const crypto = require('crypto')
const randomCode = (callback) =>{
crypto.randomInt(100000, 999999, (err, n) => {
if (err) throw err;
callback(n);
});
}
module.exports = randomCode
users.js
require('dotenv').config()
const express = require('express')
const router = express.Router()
const randomCode = require('../controllers/randomCode')
router.get('/login', async (req, res, next)=>{
try{
//-----------------------------------------------------
randomCode((code) => {
console.log(code,'code')
})
//-----------------------------------------------------
return res.send('ok')
}
catch(error){
res.send(error)
}
})
module.exports = router;
I tried to use await
but whithout results.
router.get('/login', async (req, res, next)=>{
try{
//------------------------------------------------------
const result = await randomCode((code) => {
console.log(code,'code')
})
console.log(result)
//------------------------------------------------------
return res.send('ok')
}
catch(error){
res.send(error)
}
})