Working with asynchronous callbacks - Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:
My code works as intended so far, naively adding URLs to the database (no validation or checks for existing URLs so far); but as this is my first time chaining asynchronous functions and callbacks together like this, I’d just like to ask if I’ve done it in a reasonable manner. Am I on the right track, or am I going down the wrong path?

Your code so far
I have included the relevant functions below, in the order that they are called. You may also take a look at the complete code at the link here:
https://replit.com/@b-van-b/fcc-project-urlshortener

// receive new url, shorten and return it
app.post("/api/shorturl", function (req, res) {
  const original_url = req.body.url;
  console.log("Received URL to shorten: "+original_url);
  // if url is valid
  // ...
  // and new
  // ...
  // create new shortened url and return it
  console.log("Attempting to shorten url...");
  addNewUrl(original_url, (err, data) => {
    if (err) return console.log(err);
    console.log("Successfully shortened url: "+data);
    res.json({ original_url: data.original_url, short_url: data._id });
  })
  
});

// create new url document with auto-incremented
// id representing short url and return it
const addNewUrl = (url, done) => {
  console.log("trying to add new url: "+url)
  getNextSequenceValue("url_id", (err, sequence_value) => {
    if (err) return console.log(err);
    const doc = new Url({
      _id: sequence_value,
      original_url: url      
    });
    console.log("trying to save url...");
    doc.save((err, data) => {
      if (err) return console.log(err);
      console.log("successfully saved new url: "+ data)
      done (null, data);
    });
  });
};

// increment and return the value of some sequence, such as an ID number
const getNextSequenceValue = (sequenceName, done) => {
  console.log("attempting to get next number in sequence: "+sequenceName);
  Counter.findOneAndUpdate(
    { __id: sequenceName },
    { $inc: { sequence_value: 1 } },
    { new: true },
    (err, doc) => {
      if (err) return console.log(err);
      console.log("successfully got next number in sequence: "+doc);
      done(null, doc.sequence_value);
    }
  );
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: Back End Development and APIs Projects - URL Shortener Microservice

Link to the challenge:

The code is now complete and passes all tests, but I’d still like to know if this is the proper way to chain callbacks to communicate with MongoDB or any other asynchronous tasks.

I am not sure what the proper term is. I just mean that the code is traversed all the way down to the final function, then back up to the originating function via callbacks, something like this:

app.post >> addNewUrl >> getNextSequenceValue >> findOneAndUpdate VV
app.post << addNewUrl << getNextSequenceValue << findOneAndUpdate <<

It seems like a chain of some sort to me.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.