0

I have a scenario in which i have used 'async' library in a manner as below.

async.series([function (cb) {
            dbWeb.saveOrderForm(chain.toString(), function (err, data)  {
                if (err) {
                    cb(err);
                } else {
                    req.order_id = data;
                    cb();
                }
            });
        }, function (cb) {
            async.parallel([function (cbForm) {
                async.eachSeries(formats, function (format, cbFormat) {
                    let resource = {
                        set: {
                            format_id: format,
                            order_id: req.order_id
                        }
                    };
                    let chain = sql.insert({ separator: '\n' }).into('order_format');
                    sqlify(chain, resource);
                    dbWeb.saveArray(chain.toString(), function (err) {
                        if (err) {
                            cbFormat(err);
                        } else {
                            cbFormat();
                        }
                    });
                }, function (err) {
                    if (err) {
                        cbForm(err);
                    } else {
                        cbForm();
                    }
                });
            }, function (cbForm) {
                async.eachSeries(designStyle, function (design, cbDesign) {
                    let resource = {
                        set: {
                            design_style_id: design,
                            order_id: req.order_id
                        }
                    };
                    let chain = sql.insert({ separator: '\n' }).into('order_design_style');
                    sqlify(chain, resource);

                    dbWeb.saveArray(chain.toString(), function (err) {
                        if (err) {
                            lme.e(err);
                            cbDesign(err);
                        } else {
                            cbDesign();
                        }
                    });
                }, function (err) {
                    if (err) {
                        cbForm(err);
                    } else {
                        cbForm();
                    }
                });
            }, function (cbForm) {
                async.eachSeries(fabrics, function (fabric, cbFabric){
                    let resource = {
                        set: {
                            fabric_id: fabric,
                            order_id: req.order_id
                        }
                    };
                    let chain = sql.insert({ separator: '\n' }).into('order_fabric');
                    sqlify(chain, resource);
                    dbWeb.saveArray(chain.toString(), function (err) {
                        if (err) {
                            cbFabric(err);
                        } else {
                            cbFabric();
                        }
                    });
                }, function (err){
                    if (err) {
                        cbForm(err);
                    } else {
                        cbForm();
                    }
                });
            }, function (cbForm) {
                async.eachSeries(locaion, function (loc, cbLoc){
                    let resource = {
                        set: {
                            location_id: loc,
                            order_id: req.order_id
                        }
                    };
                    let chain = sql.insert({ separator: '\n' }).into('order_location');
                    sqlify(chain, resource);
                    dbWeb.saveArray(chain.toString(), function (err) {
                        if (err) {
                            cbLoc(err);
                        } else {
                            cbLoc();
                        }
                    });
                }, function (err){
                    if (err) {
                        lme.e(err);
                        cbForm(err);
                    } else {
                        cbForm();
                    }
                });
            }, function (cbForm) {
                async.eachSeries(priceTypes, function (price, cbPrice){
                    let resource = {
                        set: {
                            order_pricing_sub_id: price,
                            order_id: req.order_id
                        }
                    };
                    let chain = sql.insert({ separator: '\n' }).into('order_price_types');
                    sqlify(chain, resource);

                    dbWeb.saveArray(chain.toString(), function (err) {
                        if (err) {
                            cbPrice(err);
                        } else {
                            cbPrice();
                        }
                    });
                }, function (err){
                    if (err) {
                        cbForm(err);
                    } else {
                        cbForm();
                    }
                });
            }, function (cbForm) {
                async.eachSeries(logos, function (logo, cbLogo){
                    let resource = {
                        set: {
                            logo_used_id: logo,
                            order_id: req.order_id
                        }
                    };
                    let chain = sql.insert({ separator: '\n' }).into('order_logo_used');
                    sqlify(chain, resource);

                    dbWeb.saveArray(chain.toString(), function (err) {
                        if (err) {
                            cbLogo(err);
                        } else {
                            cbLogo();
                        }
                    });
                }, function (err){
                    if (err) {
                        cbForm(err);
                    } else {
                        cbForm();
                    }
                });
            }
            ], function (err) {
                if (err) {
                    cb(err);
                } else {
                    cb();
                }
            });
        }
        ], function (err) {
            if (err) {
                res.status(500).json({
                    status: 'Something went wrong while trying to access data from database',
                    msg: err
                });
            } else {
                res.status(200).send({
                    status: 'success'
                });
            }
        });

This is the entire code , in first async.series function I have to save 'order' . Then I need that id , to do all the functions in 'function (cb)', those can be done in parallel. But in each parallel function , I have to do async.eachSeries.

This is the current scenario.The code feels too messy. Sorry, for the inconvenience.

How can I implement it using pg-promise functions ?

  • why don't you give names to the functions, rather than making them anonymous functions? It will be easy to propose solutions that way. – Ankit Gomkale Apr 04 '17 at 05:45
  • I am the author of `pg-promise`, but even I cannot advise you, because it is not clear to me what it is you are doing there... – vitaly-t Apr 04 '17 at 11:41

0 Answers0