How to make function synchronous in Node.js?

Hello, everybody!

I have next block in Node.js:"/api/shorturl/new",  function (req, res, next) {
 dns.lookup(res.locals.url, function(err, adresses, family){
    if (err){
      res.locals.urlValid = false;
    } else {
      res.locals.urlValid = true;

function (req, res) {
  console.log("URL valid in next? ", res.locals);
    if (res.locals.urlValid) {
      res.json({origin_url: req.body.url});
    }else {    
      res.json({error: "invalid URL"})

This code is not working, because app received response from dns.lookup() after app finish chain. So after if I include console.log() in dns.lookup(), it shows after last chain.

So my question is how to order app wait finish of dns.lookup() before handle next chain?

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.