+ 3
Node.js assyncronious stuff
Hey guys, what should I do to figure out how to handle all requests with Node.js. As example I have 2 get requests to API, and I want to save returns to a JSON file. And also, it should work even one of requests is failed. So, I can't use for it promises. I've tried axios request.all, but it fails if one of request has been failed.
20 Respostas
+ 12
//HOW ABOUT THIS??
'use strict';
const fs = exports.fs = require('fs'),
axios = exports.axios = require('axios');
var to_file = {
foo: "https://api1.com",
bar: "https://api2.com"
},
file = './file.json';
async function job(src = { }, f = file) {
for (var i in src) {
try {
src[i] = await axios.get(src[i]);
} catch(err) {
console.error(err);
}
}
return fs.writeFileSync(f, JSON.stringify(src, null, 2));
} //job
exports.job = job;
job(to_file).catch(console.error);
+ 17
So that you won't have any data overriding problems, store them in separate files initialy and wrap each promise in another promise that resolves when its inner promise fails or suceeds, and then Promise.all the outer requests and merge the separate files in one when it triggers...
+ 17
const fs = require('fs');
var url1 = "https://api1.com",
url2 = "https://api2.com",
to_file;
async function job() {
try {
var res1 = await axios.get(url1),
res2 = await axios.get(url2);
to_file = {
foo: res1,
bar: res2
};
return fs.writeFileSync('file.json', JSON.stringify(to_file, null, 2));
} catch(err) {
console.error(err);
}
} //job
job();
+ 16
Yes, with the use of async/await.
+ 16
Your code seems fine.
Storing:
const fs = require('fs');
fs.writeFileSync('file.json', JSON.stringify(to_file, null, 2));
+ 14
Promise.race
+ 13
async function requests() {
var data = [await request(url1), await request(url2), ...];
//...
} //requests
async function request(url) {
return new Promise((rsl, rjc) => {
http.get(url, cb => rsl(cb)); //...
});
} //request
requests();
+ 13
If you want further help, tell me what functions you use (http.get??), how you store data (what convenience you follow), how the structure (of the received data) is etc... And i'll do it.
+ 3
Ok, but I need to save stuff from both requests.
+ 3
Wow, I think it's too much complicated, cuz actually I need to save 12 requests. Maybe there is any way to do it not asynchronous?
ATM I use bash script for it, but I want it to be full JS solution.
+ 3
Thanks Valen.H. ~, I want something like this:
url1 = "https://api1.com"
url2 = "https://api2.com"
to_file = {}
axios.get(url1)
.then(function(response) {
if (response.data.success) {
to_file.foo = response.data.foo
}
})
.catch(function(error) {
console.log(error);
});
axios.get(url2)
.then(function(response) {
if (response.data.success) {
to_file.bar = response.data.bar
}
})
.catch(function(error) {
console.log(error);
});
console.log(to_file);
{
"foo": "foo_stuff",
"bar": "bar_stuff"
}
+ 2
Cool, "async/await" it'd definitely that I searching for. Could you make an example please? I can to google it, but I want to learn it from an alive person š
+ 2
Yeah, but it doesn't work, cuz of assync stuff =)) I have tried it, but to_file var always empty at the end of the code. Even I try to write it in a file with fs.writeSteam.
+ 2
Wow, cool, I'll try it
+ 2
It's working, but again when one of the APIs is not accesible, it's just brokes and don't save data from other requests.
+ 2
I've fixed it.
var fs = require("fs");
var axios = require("axios");
var urls = ["https://api1.com", "https://api2.com"]
async function job() {
var to_file = {};
for (var i = 0; i < urls.length; i++) {
try {
var res = await axios.get(urls[i]);
to_file[i] = res.data;
} catch (err) {
console.error(err);
}
}
return fs.writeFileSync('file.json', JSON.stringify(to_file, null, 2));
}
job();
+ 2
"var i in src" it's cool, I thought, it's only Python stuff š
+ 2
Sorry. it's work, it didn't work cuz I've run it from VSCode. But here is lil improvement:
async function job(src = {}, f = file) {
for (var i in src) {
try {
request = await axios.get(src[i]);
src[i] = request.data;
} catch (err) {
src[i] = "";
console.error(err);
}
}
return fs.writeFileSync(f, JSON.stringify(src), 'utf8');
} //job
+ 2
Awesome, I like Sololearn! I've asked this question on StackOverflow, but those nerds can't answer anything to be clear. They will told you a lot of clever words, but give you non working code.
+ 1
Data is gethered, but file don't saves.