Skip to content

Instantly share code, notes, and snippets.

@seanmars
Last active October 12, 2020 07:38
Show Gist options
  • Save seanmars/909d3beaa9b4329011b4322bcf9e43cd to your computer and use it in GitHub Desktop.
Save seanmars/909d3beaa9b4329011b4322bcf9e43cd to your computer and use it in GitHub Desktop.
const express = require('express')
const app = express()
const port = 3000
const delay = (s) => {
return new Promise(resolve => {
setTimeout(resolve, s);
});
}
class Test {
async fetchValue(value) {
await delay(3000);
return `Hello, ${value}`;
}
}
let t = new Test();
app.get('/', async (req, res) => {
var v = await t.fetchValue('12345');
res.send(v);
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
const gis = require("g-i-s");
const generateRandomAnimalName = require("random-animal-name-generator");
const randomCountryName = require("random-country-name");
const download = require("image-downloader");
const asyncGis = (opts) => {
return new Promise((resolve, reject) => {
gis(opts, (error, result) => {
if (error) {
reject();
} else {
resolve(result);
}
});
});
};
class webprocess {
async grab() {
async function logResults(error, results) {
if (error) {
console.log(error);
} else {
var rndInt = getRandomInt(3);
var imgUrl = results[rndInt].url;
console.log(imgUrl);
const options = {
url: imgUrl,
dest: "images",
};
await download
.image(options)
.then(({ filename }) => {
console.log("Saved to", filename); // saved to /path/to/dest/image.jpg
})
.catch((err) => console.error(err));
var content = {
url: imgUrl,
caption: rndInt,
};
}
console.log(content);
return content;
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
var animalNmae = await generateRandomAnimalName();
console.log(animalNmae);
var countryName = await randomCountryName.random();
console.log(countryName);
var opts_location = {
searchTerm: `pollution ${countryName}`,
};
var opts_animal = {
searchTerm: `pollution ${animalNmae}`,
};
console.log(`start location...`);
let result_location = await asyncGis(opts_location).catch(error =>{
console.log(error);
});
console.log(result_location);
console.log('end opts_location...');
console.log(`start animal...`);
let result_animal = await asyncGis(opts_animal).catch(error =>{
console.log(error);
});
console.log(result_animal);
console.log('end opts_animal...');
}
}
module.exports = webprocess;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment