I have an express endpoint that does a number of things, 1 of which is checking domain names availability against GoDaddy's API. I am not understanding how I can go about awaiting the results.
The loop will iterate my tlds
array and check the domain name availability for each of them. Currently axios will return a promise from checkDomainAvailability
, resolve, and push the data to the domains
array (I'm certain this can be written better, but I'm not well versed in JS).
app.post("/domains", async (req, res) => {
var brand = req.body.brand;
let domains = [];
// check domain availability
const tlds = ["dev", "com", "tv", "io", "app", "me"];
tlds.forEach(tld => {
let domain = `${brand}.${tld}`;
godaddy.checkDomainAvailability(domain).then(data => {
domains.push(data);
});
});
res.send(domains);
}
godaddy.checkDomainAvailability()
const checkDomainAvailability = function(domain) {
return api
.get("/domains/available", {
params: {
domain: domain,
checkType: "FULL",
forTransfer: false
}
})
.then(res => {
return res.data;
})
.catch(err => {
logger.error("Godaddy", err);
});
};
The current result is obviously that express sends the result of domains before each has been iterated. How can I make this await the forEach loop?
I have attempted using for await
as follows but seeing the same results.
// check domain availability
const tlds = ["dev", "com", "tv", "io", "app", "me"];
for await (let tld of tlds) {
let domain = `${brand}.${tld}`;
godaddy.checkDomainAvailability(domain).then(data => {
domains.push(data);
});
}