synchronizing node.js calls

synchronizing node.js calls



I am writing my first "real" AWS lambda application, and I need to synchronize a couple calls. There don't appear to be "sync" versions of the methods I am calling.



Specifically, I want to make a call to Dynamo, and then use data parsed from the JSON to make an http request.



After having set it up, here's my working Dynamo call...


dynamoDb.get(params, function(err, data)
if (err)
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
else
url = parse(data);
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));

);



I then call an http request using that url...


http.get(url,function(res)
res.on('error', function(err)
console.log(err);
);

res.on('data',function(d)
console.log("received more chunks");
chunks += d;
);

res.on('end',function()
console.log("http request complete");
)



The problem seems to be that the second call doesn't wait for the first one. I have looked into using async/await, but I am not sure how I use those with the error handling callbacks. Examples I have found online are either too general and hard for me to comprehend, or specific without the error handling.



Any idea how I sync these two?





You've proposed a very reasonable, very common sequence of server-resource interactions here. Modern asynchronous methods in Node.js--including async/await and Promises--were built precisely to deal with cases like this. And I bet this won't be your last AWS lambda call. I hope you learn about these useful techniques in the future!
– Andy Taton
Sep 3 at 0:08




1 Answer
1



Ignoring async/await for the moment, have you tried:


dynamoDb.get(params, function(err, data)
if (err)
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
else
url = parse(data);
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
http.get(url,function(res)
res.on('error', function(err)
console.log(err);
);

res.on('data',function(d)
console.log("received more chunks");
chunks += d;
);

res.on('end',function()
console.log("http request complete");
);
);

);





Syntax error missing closing parenthesis on that http request
– Darkrum
Sep 2 at 19:23





I have tried that, but my example above was only for clarity's sake. I have a much more complicated program, and would also like to understand why this is not working.
– user870
Sep 10 at 23:09



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)