@s-projects18 (S Projects18) posted a new topic, subject "Question about handling database-queries in mongoose/mongodb properly". It read:

   **Question** : what is the recommended way to handle a sequence of database operations where later operations are depending on the result of the former?
    
    An example snippet for a database update:
    [code] 
          let short_url = -1;
          Counter.findOneAndUpdate({sequence_value:{$gt:0}}, {$inc:{sequence_value:1}}, {}, function(err, docs){
            short_url=docs.sequence_value;
            console.log(111, short_url);
          });
          console.log(222, short_url);
        
    [/code]
    
    The result in console is:
    [code] 
        222, -1
        111, 42
        
    [/code]
    
    **Problem** : the new updated value is not yet available when I make the 2nd console.log().  
    (I'm coming from PHP/MySQL and there it's working different: execution will not continue until an database-operation is finished)
    
    I found e.g. this tutorial using async/await:  
    https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016
    
    But I am not sure whether this is the right way

After which, @willjw3 (Will W) added

    How about wrapping everything in an outer function and using async/await?
    [code] 
        async function myFunction() {
           const blahs = await bleh.doStuff(blah => {
               console.log(blah);
               return blah;
          });
          console.log(blahs)
        }
        
    [/code]

@s-projects18 (S Projects18) replied

    I will try that, it sounds logic.

Then @ilenia (Ilenia) said

    


{'source': 'discourse', 'url': 'https://forum.freecodecamp.org/t/question-about-handling-database-queries-in-mongoose-mongodb-properly/296877', 'title': 'Question about handling database-queries in mongoose/mongodb properly', 'views': 256, 'has_accepted_answer': False, 'word_count': 185, 'like_count': 1, 'reply_count': 1, 'participant_count': 2, 'posts': 4, 'tags': '', 'author': '@s-projects18 (S Projects18)', 'participants': '@s-projects18 (S Projects18), @willjw3 (Will W), @ilenia (Ilenia)', 'image_url': [None], 'created_at': '2019-07-20T06:58:35.904Z', 'last_posted_at': '2021-01-25T12:24:52.180Z'}