Node.js DynamoDB callback I am writing an API which gets results from a DynamoDB table and puts the JSON back into the browser.

The code below works and returns the desired results.  However, after reading about async and callbacks, it's becoming important for me to know if I should be writing this in another way.  Does the following scale well with hundreds of concurrent API callers?

This code does not seem to be using callbacks.  Is it asynchronous?

var restify = require('restify');
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});
var db = new AWS.DynamoDB();

function myFunction(req, res, next) {
  var params = {
    TableName : 'myTable',
    KeyConditions : 
      {
        "number" : 
          {
            "AttributeValueList" : [
              {
                "S" : req.params.simethingid
              }
            ],
            "ComparisonOperator" : "EQ"
          }
      }
  }

  db.query(params, function(err, data) {
    if (err) {
      console.log(err);
      } 
    else {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
      res.send(JSON.stringify(data, undefined, 2));
      console.log(JSON.stringify(data, undefined, 2))
      res.end();
      }
  });
  return next();
 }

server.get('/searchme/:somethingid', myFunction);
Can't speak to how it'll scale, but for your 2nd question: Yes, the code above is asynchronous.

When you call db.query() or server.get() you pass a callback as the 2nd argument. That's the asynchronous stuff, as you don't know when that callback will be invoked.

You basically just say "call me when it's done" and continue about your business (i.e. in the case of db.query() the evaluation will proceed to return next(); without "waiting" for the query to finish).

If it were synchronous, it'd be something like result = db.query(params); where everything would wait for the query to return something to assign to result.

By the way (so this is actually a review and not just an explanation)


fix your indentation
in JS it's better to not use the brace-on-new-line style
you're missing some semi-colons
you don't need to quote all those property names in the params object
beware of using undefined, since that isn't actually a keyword. Some runtimes let you say undefined = 23 in which case undefined is then just another (defined) variable. Use null instead