Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Using SQL ORMs with Node.js Performing CRUD Operations Delete a Record

JASON LEE
JASON LEE
15,783 Points

Alternate solution using Promises instead of async/await

Using Async await

    // Find a record
    const toyStory = await Movie.findByPk(1);

    // Delete a record
    await toyStory.destroy();

Using Promise (with catch clause)

   // Find and Delete a record 
   Movie.findByPk(5).then(result => result.destroy())
      .catch(()=>{console.log("An error occurred in destroying the record.")});

My (newbie) understanding is that async-await is newer and supersedes Promises, but it's good to be familiar with both. I noticed a key difference here is that with async-await -> when a Primary Key record doesn't exist in the database, the program will crash. However with Promises, the .catch() clause can prevent crashing.

1 Answer

Steven Parker
Steven Parker
218,692 Points

The async/await syntax is used with promises, it does not superseded them. But you're right about it being newer, promises were released first and required complicated chaining to achieve the same functionality that async/await now simplifies.