Home Nodejs function keeps executing even after "return callback()" is called
Reply: 0

Nodejs function keeps executing even after "return callback()" is called

user1410
1#
user1410 Published in June 23, 2018, 9:23 pm

In the following function, it is my understanding that the code should stop executing after the callback if either of the first two if conditions is met, i.e. if either "validate_post" returns an error or if "!clean.postId == true". When I ran into the second if condition during testing, the callback was called with the error message, however the following block (data.updatePost()) was still executed, resulting in the callback being called again. What am I doing wrong here? Thanks for your help!

    var data = require('./data')
    var validate_post = require('./validate_post')

    module.exports = function(post, callback) {
      validate_post(post, function(err, clean) {
        if(err) {
          callback({status: "error", message: err})
          return
        }

        if(!clean.postId) {
          console.log('first callback is called at this point')
          callback({status: "error", message: "Something is off here"})
          return
        }

        console.log('code still executes for some reason')
        data.updatePost(clean.postId, clean.post, function(err, res) {
          if (err) {
            callback({status: "error", message: err})
            return
          }
          console.log('callback is called a second time at this point')
          callback(null, {status: "success", message: res})
        })
      })
    }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.32333 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO