javascript - How to avoid callback hell in this scenario? -


i have following code can see, each of functions inside functions rely on return value of enclosing function. problem when keep using method code, callback hell occurs. how avoid hell?

user.getuserdetail(req.user.id, function(userdetail) {        if(req.user.entity_id != '-1') {            entity.getentityprimary(req.user.entity_id, function(entityprimary) {                entity.getentitypayment(req.user.entity_id, function(entitypayment) {                    if(entitypayment.length > 0) {                        entity.subscriptioninfo(entitypayment[0]['date'], entitypayment[0]['exp_date'], function(issubscribed) {                            res.render('capitol', {                                user: req.user,                                title: 'mcic',                                user_detail: userdetail,                                entity_primary: entityprimary,                                entity_payment: entitypayment,                                subscriber: true,                                is_subscribed: issubscribed                            })                        })                    } else {                        res.render('capitol', {                            user: req.user,                            title: 'mcic',                            user_detail: userdetail,                            entity_primary: entityprimary,                            entity_payment: entitypayment,                            subscriber: false                        })                    }                })            })        } else {            res.render('capitol', {                user: req.user,                title: 'mcic',                user_detail: userdetail            })        }    }) 

the model file follows

const mysql = require('../comms/mysql') const user = module.exports = {}  user.getuserbyusername = function(username, callback) {     mysql.connection.query('select id, username, password, is_active, email, mobile, user_type_id, entity_id `user` `username` = ?', username, function(err, rows, fields) {         callback(rows)     }) }  user.getuserbyid = function(id, callback) {     mysql.connection.query('select id, username, password, is_active, email, mobile, user_type_id, entity_id `user` `id` = ?', id, function(err, rows, fields) {         callback(err, rows)     }) }  user.getuserdetail = function(id, callback) {     mysql.connection.query('select first_name, last_name, dob, address_no, address_street, address_district, address_postal_code, profile_picture, user_id `user_detail` `user_id` = ?', id, function(err, rows, fields) {         callback(rows)     }) } 

i'm using code in production website. how can transition callback hell structured code?

use javascript promises

step1(function (value1) {     step2(value1, function(value2) {         step3(value2, function(value3) {             step4(value3, function(value4) {                 // value4             });         });     }); }); 

can made

q.fcall(promisedstep1) .then(promisedstep2) .then(promisedstep3) .then(promisedstep4) .then(function (value4) {     // value4 }) .catch(function (error) {     // handle error above steps }) 

read more here link q.js


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -