I want to write a small worker app in node.js.
This app would read from AWS-SQS, process the data in some way, and spit it out to another AWS-SQS.
So far I have written:  
while(true){
    readFromQueue()
    .then(transform)
    .then(writeToQueue);
}
function transform(data) {
    console.log("> transforming...");
    //transformation logic
    return data;
}
//TODO: need to remove message from queue after read!
function readFromQueue() {
    // var params = {
    //   QueueUrl: 'STRING_VALUE',
    //   WaitTimeSeconds: 2
    // };
    // return new Promise(function(resolve, reject) {
    //  sqs.receiveMessage(params, function(err, data) {
    //      if (err) reject(err);
    //      else     resolve(data);
    //  });
    // });
    return new Promise(function(resolve, reject) {
        console.log("> reading from queue...");
        resolve({ data : "world" });
    });
}
function writeToQueue(data) {
    // var params = {
    //  MessageBody: data,
    //  QueueUrl: 'STRING_VALUE',
    // };
    // sqs.sendMessage(params, function(err, data) {
    //  if (err) console.log(err, err.stack);
    //  else     console.log(data);
    // });
    console.log("> writing to queue...");
    console.log(">> " + data);
}
As you can see everything is set up for AWS, but when I run it locally for the time being, I would just have some mock stuff inside, until I actually get my transformation logic tested etc...
The problems I have are:  
- AWS-SQS API is async, it takes a callback. I wrapped it with a promise because I prefer promises to callbacks. Nevertheless, I wonder if I should block it and make it sync rather than async (for the reason below, but might be unrelated).
- When I run this, I can see only "reading from queue.." output, it's almost as if "transform" and "writeToQueue" are not executed...
- If I, however, comment out the while loop (so the script runs only once), I can see output from all 3 steps.
So am I doing something wrong? I can understand that since promises are async, my while loop will go crazy and create thousands of them, so that concerns me... Nevertheless I want to initiate another loop once the previous read->transform->write has been finished. Is there some other pattern I should use here? Or just block and wait for readFromQueue to end...
--EDIT--
It does execute everything if not wrapped in while(true):
readFromQueue()
        .then(transform)
        .then(writeToQueue);
I also understand that since while(true) is being executed, it will essentially block the thread, and therefore the promise is not resolved. So is there a way around it?
 
     
     
     
    