What is a good way to retrying providing response in node.js several times?
-
The case is, that I have a method, that is doing one of three things. It get's an object from the database. If it not present then creates it but with empty url field. After that it generates an image, which can take a pretty long time (lets say 2s), upload it to s3 (another 2s) and if upload is finished add the url to the object, or if not, deletes that object. If the object exists but it doesn't have url, it SHOULD retry all after 500ms for example. If the object url is present, stream the file from that url. I was trying to use setTimeout for that, but if I have 1000 connections at the same time, I get Error: accept EMFILE, (to many files opened) var refresh = false; module.exports.imageGet = function(req, res, next){ if (!refresh) { refresh = true; res.send('ok'); } else { var timeout = setTimeout(module.exports.imageGet,500,req,res,next); } }; It's not about timeout cleaning, because I had a code that was cleaning them and It didn't help. It seams that the setTimeout by itself makes that works bad.
-
Answer:
Take a look at the asyncJs package: https://github.com/caolan/async/blob/master/README.md#retry There's an ongoing debate between promise based flows and libraries such as async or step. I'll leave that up to you.
Ruben Cordeiro at Quora Visit the source
Other answers
eeek...don't use setTimeout look at the npm package q
Jonathan Jaffe
Related Q & A:
- What is a good way to structure mark-up generating code and avoid the example mess?Best solution by Code Review
- What is a good way to advertise enexpensively for a small business? It is handyman work?Best solution by Yahoo! Answers
- What's a good way to let a girl know you are interested in her?Best solution by Yahoo! Answers
- What is a good way to break in a snowboard?Best solution by answers.yahoo.com
- What is a good way to promote a movie?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.