0

I have a scenario where I have an image from a user on a mobile device and some accompanying data. I'm using Amazon Web Services and the idea was to store the image in S3 and the data into a database table.

I'm processing this data using Node JS.

To store the image onto S3, I am using the AWS SDK. To store the record into MySQL I am using the 'mysql' NPM package.

The Problem

What is the best practice way to ensure both events run smoothly, and if not, make sure none take place. So let's imagine the S3 putObject has finished and was error free, but the MySQL insert timed out.

The Solution

I can come up with many - and have done in the past. The real problem here is what I would consider best practice, and after researching on stack overflow, I can't seem to find a good scenario or best practice answer to the question:

"What is the best way to handle errors when a single function involves two or more (potentially error prone) remote dependencies?"

I was just wondering if anybody had the answer to this?

Thanks all

user1873468
  • 477
  • 1
  • 4
  • 11
  • "best way" is a problematic phrase on Stack Overflow. It invites opinions, especially when unbounded like your question is. You've also not shown what you have done in the past, so we can't tell if we're giving you an answer you already know about. See also http://stackoverflow.com/q/7310521/215552 – Heretic Monkey Mar 01 '17 at 15:45

1 Answers1

0

Good question , the best practice is to use asynchronous triggering. This simply means , dont do this,or that until a event is triggers granting such action.In the realm of node custom event streams using the event package is one way to go. Simply create a event manually, then listen for a certain response and then fire that accordingly, this ensures only what you want is executed.The best way to go is to use the concept of promisies, Promises provide a simpler alternative for executing, composing, and managing asynchronous operations when compared to traditional callback-based approaches. They also allow you to handle asynchronous errors using approaches that are similar to synchronous try/catch. Try using the bluebird for node.https://alexperry.io/node/2015/03/25/promises-in-node.html

Remario
  • 3,813
  • 2
  • 18
  • 25