-1

I have a simple express server that is running a loop checking every domain in an array.. Right now I'm pushing all of the results into an array results on every .then statement of the promise. How would I then send the results in server response after this promise block completes? Should I run a callback with res.send(results) after the promise? Or is there a way to do it from inside the promise with .finally?? Or should i be using the express next parameter? I'm not sure how to do this.

const whois = require('whois-info');
const express = require('express');
const app = express();

app.get('/domainfinder/domain/:domainURL', (req, res) => {
  //const domainURLs = req.params.domainURLs;

  let tests = ['google.com', 'nomatchdomain.com', 'notfounddomain.me'];

  let results = [];

  [...tests].forEach(domain => {
    whois.lookup(domain)
      .then(data => results.push(data))
      .catch(e => console.log(domain, e.message))
  });

  res.send(results);
});

app.listen(3000, () => console.log('App listening on port 3000!'));
AGrush
  • 1,107
  • 13
  • 32
  • are you facing any problem or you are looking for best practice? – Amit Tiwary Jan 01 '20 at 16:49
  • You should use Promise.all or await the requests, you currently have **no way** to tell when all the results are available. – jonrsharpe Jan 01 '20 at 16:49
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – jonrsharpe Jan 01 '20 at 16:49
  • You can use `Promise.all` if you don't mind doing all the lookups at once, or you could [use `.reduce`](https://gist.github.com/anvk/5602ec398e4fdc521e2bf9940fd90f84) to do them one by one and roll everything into one promise. Better yet, since this is presumably your own server, you could use `async`/`await` and a regular `for...of` loop. – JLRishe Jan 01 '20 at 16:50

2 Answers2

3

You have to call next(err) in case of error, and res.send(results) in the then callback:

const whois = require('whois-info');
const express = require('express');
const app = express();

app.get('/domainfinder/domain/:domainURL', (req, res) => {
  //const domainURLs = req.params.domainURLs;

  let tests = ['google.com', 'nomatchdomain.com', 'notfounddomain.me'];

  Promise.all(tests.map(domain => whois.lookup(domain)))
      .then(results => res.send(results))
      .catch(e => next(e))
  });
});

app.listen(3000, () => console.log('App listening on port 3000!'));
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
2

Three options:

Use async/await (executes lookups one by one):

const whois = require('whois-info');
const express = require('express');
const app = express();

app.get('/domainfinder/domain/:domainURL', async (req, res) => {
  //const domainURLs = req.params.domainURLs;

  let tests = ['google.com', 'nomatchdomain.com', 'notfounddomain.me'];

  try {
    let results = [];

    for (const domain of tests) {
      results.push(await whois.lookup(domain));
    }

    res.send(results);
  } catch (e) {
    // handle error
  }
});

app.listen(3000, () => console.log('App listening on port 3000!'));

Use Promise.all (executes all lookups at once):

const whois = require('whois-info');
const express = require('express');
const app = express();

app.get('/domainfinder/domain/:domainURL', (req, res) => {
  //const domainURLs = req.params.domainURLs;

  let tests = ['google.com', 'nomatchdomain.com', 'notfounddomain.me'];

  Promise
    .all(tests.map(domain => whois.lookup(domain)))
    .then(results => res.send(results)
    .catch(e => { /* handle error */ });
});

app.listen(3000, () => console.log('App listening on port 3000!'));

Use reduce (executes lookups one by one):

const whois = require('whois-info');
const express = require('express');
const app = express();

app.get('/domainfinder/domain/:domainURL', (req, res) => {
  //const domainURLs = req.params.domainURLs;

  let tests = ['google.com', 'nomatchdomain.com', 'notfounddomain.me'];

  tests.reduce(
    (p, domain) =>
      p.then(results => whois.lookup(domain).then(data => [...results, data])),
    Promise.resolve([])
  )
  .then(results => res.send(results))
  .catch(e => { /* handle error */ });
});

app.listen(3000, () => console.log('App listening on port 3000!'));
JLRishe
  • 99,490
  • 19
  • 131
  • 169