O
O
Oleg Gamega2017-06-08 13:53:47
JavaScript
Oleg Gamega, 2017-06-08 13:53:47

How to get away from callback hell in node.js?

Hello.
I must say right away that everything below does not belittle node.js in any way, I generally have a negative attitude towards jokes about php / 1c developers, I have quite a positive attitude towards js, especially es6 =)

There are only two types of programming languages: those that people swear at all the time, and those that no one uses.

I like asp, but reality pushes hard to node (this is the fault of the react stack, many clients want a node backend)
What I didn’t like about node:
callback hell
orm a lot
needs to be done manually
for projects you like.
Thank you.
update:
generators, promises, async/await as if for granted, but I would like examples of using all this and relevant libraries,
I was rightly criticized at one time for criticizing one popular orm, they say that it’s ancient, although there are a lot of stars, updates are regular, recommendations this orm is pretty fresh

Answer the question

In order to leave comments, you need to log in

5 answer(s)
N
Negwereth, 2017-06-08
@Negwereth

well, promises, and now also async/await, well

E
emp1re, 2017-06-08
@emp1re

callback is our everything, the rest is decay

I
Igor Peregudov, 2017-06-08
@igorperegudov

es6 and promises? :)

A
Anton Anton, 2017-06-08
@Fragster

Promise (+ async/await for node 8, which will be LTS, I'll switch to it in a couple of months from node 6), sequelize as ORM. Express as a router.

D
de1m, 2017-06-08
@de1m

Since we are talking about all this noodles, maybe someone can explain to me.
I have several actions following each other, and I have to wait until the previous operation is completed before I can do more.
1. Check if the ssh connection is
working 2. Check if the ssh connection to another server is working
3. Run the next step
Function:

backupRun.prototype.checkSSHConnection = async function (serverName) {
    var self = this;
    var exec = self.bConfig.clientserver[self.eServer]
    var client = self.bConfig.clientserver[serverName]

    return new Promise((resolve, reject) => {
        self.runUnderSSH("hostname", self.sshkey, exec, client)
            .then((sshOutput) => {
                return resolve(sshOutput);
            }, (sshOutputErr) => {
                return reject(sshOutputErr)
            })
    })

};

To go through all three steps I have done this
backupJob.checkSSHConnection(program.exec) //check ssh to exec server
          .then((response) => {
            logger.debug("SSH test connection to exec " + program.exec + " successfull");

            backupJob.checkSSHConnection(program.server) //check ssh connection to backup server
              .then((response) => {
                logger.debug("SSH test connection to backup server " + program.exec + " successfull");

                backupJob.createBackupPrePostCommands('prerun') //create backup server prerun commands
                  .then((backupPreRun) => {
                    logger.debug("Create backup prerun commands");
                          }, (err) => {
                            logger.error("Error: config array was not generated " + err);
                          })
                      })
                  })
              }, (error) => {
                logger.error("SSH test connection to backup server " + program.exec + " failed: " + error);
              })
          }, (error) => {
            logger.error("SSH test connection to exec " + program.exec + " failed: " + error);
          });

That is the same noodles, but only with async/await.
Now the actual question is, is it possible to do this somehow differently, or is there no way out of this? And it all looks about the same with callback, promise and async/await.
You can see it all here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question