0

I am a newbie in Node.JS. I am trying to fetch data from an api and filtering out data from a miscellaneous response. But my while loop is getting into infinite loop as the res array never gets populated due asynchronous behavior of request.

let res=[];
    while(res.length<=10){
        request(searchLink.apiLink, (err, response, body)=>{
            console.log("Agregation complete!");
            if(err){
                throw new Error("Api response error detected : ", err);
            }else{
                body=JSON.parse(body);
                for(let i=0; i<body.results.length; i++){
                    var cond=body.results[i].title!=null && body.results[i].description!=null && body.results[i].content!=null && body.results[i].language=="english";
                    if(cond) res.push(body.results[i]);
                }
                searchLink.apiLink+=`&page=${body.nextPage}`;
            }
        });
    }

Promisifying and awaiting is not quite solving the problem as express wants standard functions for middleware and not promises. And as far as I know top level await should be allowed to settle the request but Node is throwing syntax errors.

  • `express wants standard functions` `async` functions are standard functions, the only difference is they return a promise, and you can use them in middleware's in expresss. – Keith May 09 '23 at 15:43
  • I mean express.use() isn't taking in any promises for arguments. So i have to settle the request operation within each iteration. How? – Sourabrata Bose May 09 '23 at 15:57
  • What do you mean `expess.use` isn't taking any promises as arguments,.. This will work -> `express.use(async (req, res, next) => { .... await something().. etc ` – Keith May 09 '23 at 15:58
  • But I do have to settle them somewhere right? await isn't settling the promises passed on from middle ware to middleware. await can resolve and res.send the settled promise from async functions right? – Sourabrata Bose May 09 '23 at 16:03
  • The middlewares pass along using the `next` callback, you have full control of this. eg.. `app.use(async (req, res, next) => { await doSomthing(); next(); })` Pretty much all my express routes use `async / await`, and they work like a charm. – Keith May 09 '23 at 16:05
  • Exactly the problem is my await doSomething() is throwing syntax error. IDK why. doSomething() is an custom defined async function which contains the code I posted. – Sourabrata Bose May 09 '23 at 16:07
  • That's fine, you need to trap for errors, just like any other code, something like ->. `app.use(async (req, res, next) => { try { await doSomething(); next(); } catch (e) { next(e); })` `next(e)` just passes the error to the next middleware, – Keith May 09 '23 at 16:09
  • `app.get('/news',(req, res)=>{ res.send(await settings()); });` This should work right? The settings function is async-ed but the interpreter wont run since await isnt top level of the module or inside a async function. – Sourabrata Bose May 09 '23 at 16:11
  • `app.get('/news',async (req, res)=>{ res.send(await settings()); })` Don't forget the `async` or you don't get `await`.. – Keith May 09 '23 at 16:12
  • 1
    IT WORKS !! Thanks a lot. Damn 1 async function and the whole code branches off into asyncs. What if I wanted to synchronously run the code after the request ? – Sourabrata Bose May 09 '23 at 16:16
  • Not 100% sure what you mean, run the code after the request?. You maybe need to update question to show an example. To add `synchronously` for performance reasons sync code in an express app wants to be kept to the minimal.. – Keith May 09 '23 at 16:19
  • 1
    `request` and `request-promise` have been deprecated for many years by the way. You must have seen those in old tutorials. It's best to read more recent materials. – Aurast May 09 '23 at 22:13
  • I started learning JavaScript and node a week ago :( . Long way to go. – Sourabrata Bose May 10 '23 at 08:54
  • I'd use `fetch` or `axios`, not `request` or `request-promise`. – ggorlen May 11 '23 at 00:06

0 Answers0