Javascript 节点JS蓝鸟承诺

Javascript 节点JS蓝鸟承诺,javascript,node.js,promise,Javascript,Node.js,Promise,我正在用NodeJS创建承诺。但我有一个想法,我仍然有一个地狱正在进行 这是因为我需要第一次回调的结果,在第三次回调中 我已经用蓝知更鸟试过了,但我对此有些困惑 有没有人能给我一些示例代码,我怎样才能很好地写出来? 请参见以下示例: 使用async/await避免嵌套承诺 您可以将代码重构为类似这样的内容 async function doSomething(){ try { const user = await UserProfileMatch.getNewUser(i

我正在用NodeJS创建承诺。但我有一个想法,我仍然有一个地狱正在进行

这是因为我需要第一次回调的结果,在第三次回调中

我已经用蓝知更鸟试过了,但我对此有些困惑

有没有人能给我一些示例代码,我怎样才能很好地写出来? 请参见以下示例:
使用async/await避免嵌套承诺

您可以将代码重构为类似这样的内容

async function doSomething(){
    try {
        const user = await UserProfileMatch.getNewUser(item.fk_user_id)
        await ActiveAuctionProfile.closeActiveAuctionProfile(item.aap_id)

        if(user.length  > 0 ) {
            const is_active = await ActiveAuctionProfile.isProfileActive(user[0].profile_id)

            const number_of_profiles = await = UserProfileMatch.countActiveProfilesForUser(item.fk_user_id)

            if(is_active.result === 0 && number_of_profiles < config.settings.lovingbids_start_profile) {

                await UserProfileMatch.updateProfileMatch(item.fk_user_id, user[0].profile_id,1,false)

                await ActiveAuctionProfile.createNewActiveProfile({
                    fk_auction_profile_id:user[0].profile_id,
                    start_date:moment().format("YYYY-MM-DD HH:mm:ss") ,
                    expire_date:moment().add(config.settings.increment_settings,'seconds').format("YYYY-MM-DD HH:mm:ss")
                })

                ExpireProfileRegister.removeExpireResult(item.id);
                page++;
                next();

            } else {
                console.log("exists");
                ExpireProfileRegister.removeExpireResult(item.id);
                page++;
                next();
            }

        } else {
            console.log("niet");
            page++;
            next();
        }

    }catch(err){
       console.log("One of the promises failed:", err)
    }
}
异步函数doSomething(){ 试一试{ const user=await UserProfileMatch.getNewUser(item.fk\u user\u id) 等待ActiveAuctionProfile.closeActiveAuctionProfile(item.aap\u id) 如果(user.length>0){ const is\u active=wait ActiveAuctionProfile.isProfileActive(用户[0]。配置文件\u id) const number_of_profiles=wait=UserProfileMatch.countactiveprofilesfourser(item.fk_user_id) if(is_active.result==0&&number_的配置文件 请注意,我们使用
async
声明包装函数,并使用wait命令异步函数在运行下一行代码之前等待该函数完成,而不是嵌套回调

还要注意,所有等待函数都包装在try/catch块中,以捕获任何错误。这不是使用
.catch()
符号


阅读有关异步函数的更多信息

我无法调试,但根据这些内容,您应该可以正确使用承诺重构代码

UserProfileMatch.getNewUser(item.fk_user_id)
                .then(user => ActiveAuctionProfile.closeActiveAuctionProfile(item.aap_id))
                .then(() => user.length  > 0 ? ActiveAuctionProfile.isProfileActive(user[0].profile_id)
                                             : (console.log("niet"), page++, next()))
                .then(is_active => [UserProfileMatch.countActiveProfilesForUser(item.fk_user_id), is_active.result])
                .then(([number_of_profiles,n]) => n === 0 &&
                                                  number_of_profiles < config.settings.lovingbids_start_profile ? UserProfileMatch.updateProfileMatch(item.fk_user_id, user[0].profile_id,1,false))
                                                                                                                : (console.log("exists"),
                                                                                                                   ExpireProfileRegister.removeExpireResult(item.id),
                                                                                                                   page++,
                                                                                                                   next());
                .then(() => ActiveAuctionProfile.createNewActiveProfile({ fk_auction_profile_id: user[0].profile_id,
                                                                          start_date           : moment().format("YYYY-MM-DD HH:mm:ss") ,
                                                                          expire_date          : moment().add(config.settings.increment_settings,'seconds').format("YYYY-MM-DD HH:mm:ss")
                                                                        })
                .then(() => (ExpireProfileRegister.removeExpireResult(item.id), page++, next()));

它携带
是活动的。结果
到下一个
然后
阶段,在那里我们通过数组分解将其收集为
n

非常感谢大家的反馈。我读过关于
async
async.series
的书,这似乎是我最好的选择。它使我的代码保持结构化。看

                            var get_user;
                            var user_active;
                            var user_profile;
                            async.series([

                                // check for the new user
                                function checkForNewUser(callback)
                                {
                                    UserProfileMatch.getNewUser(item.fk_user_id).then(function (user) {
                                        get_user = user;
                                        callback(null,user);

                                    });
                                },
                                // close the profile
                                function closeProfile(callback)
                                {
                                    ActiveAuctionProfile.closeActiveAuctionProfile(item.aap_id).then(function () {
                                        if (get_user.length > 0) {
                                            ActiveAuctionProfile.isProfileActive(get_user[0].profile_id).then(function (is_active) {
                                                user_active = is_active.result;
                                                callback(null,user_active);
                                            });
                                        } else {
                                            callback(null,false);
                                        }

                                    });

                                },

                                // count the active profiles
                                function countActiveProfiles (callback) {

                                    UserProfileMatch.countActiveProfilesForUser(item.fk_user_id).then(function (number_of_profiles) {
                                        user_profile = number_of_profiles;
                                        callback(null,user_profile);
                                    });
                                },

                                // decide when we want to create an user
                                function determineCreation(callback) {

                                    if(user_active === 0 && user_active === 0) {
                                        UserProfileMatch.updateProfileMatch(item.fk_user_id, get_user[0].profile_id, 1, false).then(function () {
                                            ActiveAuctionProfile.createNewActiveProfile({
                                                fk_auction_profile_id: get_user[0].profile_id,
                                                start_date: moment().format("YYYY-MM-DD HH:mm:ss"),
                                                expire_date: moment().add(config.settings.increment_settings, 'seconds').format("YYYY-MM-DD HH:mm:ss")
                                            }).then(function () {
                                                ExpireProfileRegister.removeExpireResult(item.id);
                                                callback(null,true);
                                            });

                                        });
                                    } else {
                                        ExpireProfileRegister.removeExpireResult(item.id);
                                        callback(null,true);
                                    }
                                }

                            ], function(err,res) {
                                // after done, go to the next page
                                page++;
                                next();
                            });

理解和回答问题所需的代码必须粘贴到问题本身中,然后适当格式化。您也可以提供外部链接参考,但它们不能作为理解您的问题的主要手段。这是因为外部引用往往会随着时间的推移而改变或消失,从而使原始问题无法作为长期引用(这是stack overflow的目标之一)。此外,请选择一个更相关的标题来描述您所问的实际问题。你的标题非常通用。我喜欢这个缩进。
                            var get_user;
                            var user_active;
                            var user_profile;
                            async.series([

                                // check for the new user
                                function checkForNewUser(callback)
                                {
                                    UserProfileMatch.getNewUser(item.fk_user_id).then(function (user) {
                                        get_user = user;
                                        callback(null,user);

                                    });
                                },
                                // close the profile
                                function closeProfile(callback)
                                {
                                    ActiveAuctionProfile.closeActiveAuctionProfile(item.aap_id).then(function () {
                                        if (get_user.length > 0) {
                                            ActiveAuctionProfile.isProfileActive(get_user[0].profile_id).then(function (is_active) {
                                                user_active = is_active.result;
                                                callback(null,user_active);
                                            });
                                        } else {
                                            callback(null,false);
                                        }

                                    });

                                },

                                // count the active profiles
                                function countActiveProfiles (callback) {

                                    UserProfileMatch.countActiveProfilesForUser(item.fk_user_id).then(function (number_of_profiles) {
                                        user_profile = number_of_profiles;
                                        callback(null,user_profile);
                                    });
                                },

                                // decide when we want to create an user
                                function determineCreation(callback) {

                                    if(user_active === 0 && user_active === 0) {
                                        UserProfileMatch.updateProfileMatch(item.fk_user_id, get_user[0].profile_id, 1, false).then(function () {
                                            ActiveAuctionProfile.createNewActiveProfile({
                                                fk_auction_profile_id: get_user[0].profile_id,
                                                start_date: moment().format("YYYY-MM-DD HH:mm:ss"),
                                                expire_date: moment().add(config.settings.increment_settings, 'seconds').format("YYYY-MM-DD HH:mm:ss")
                                            }).then(function () {
                                                ExpireProfileRegister.removeExpireResult(item.id);
                                                callback(null,true);
                                            });

                                        });
                                    } else {
                                        ExpireProfileRegister.removeExpireResult(item.id);
                                        callback(null,true);
                                    }
                                }

                            ], function(err,res) {
                                // after done, go to the next page
                                page++;
                                next();
                            });