+ 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.

19th Jun 2018, 7:43 PM
LoooooooL
LoooooooL - avatar
20 Réponses
+ 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);
27th Jun 2018, 1:18 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 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...
20th Jun 2018, 6:02 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 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();
21st Jun 2018, 11:11 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 16
Yes, with the use of async/await.
20th Jun 2018, 10:50 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 16
Your code seems fine. Storing: const fs = require('fs'); fs.writeFileSync('file.json', JSON.stringify(to_file, null, 2));
21st Jun 2018, 11:00 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 14
Promise.race
19th Jun 2018, 8:23 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 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();
20th Jun 2018, 1:50 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 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.
20th Jun 2018, 1:53 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
Ok, but I need to save stuff from both requests.
19th Jun 2018, 9:01 PM
LoooooooL
LoooooooL - avatar
+ 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.
20th Jun 2018, 7:34 AM
LoooooooL
LoooooooL - avatar
+ 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" }
21st Jun 2018, 8:08 AM
LoooooooL
LoooooooL - avatar
+ 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 😀
20th Jun 2018, 1:32 PM
LoooooooL
LoooooooL - avatar
+ 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.
21st Jun 2018, 11:02 AM
LoooooooL
LoooooooL - avatar
+ 2
Wow, cool, I'll try it
21st Jun 2018, 11:13 AM
LoooooooL
LoooooooL - avatar
+ 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.
27th Jun 2018, 12:47 PM
LoooooooL
LoooooooL - avatar
+ 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();
27th Jun 2018, 1:15 PM
LoooooooL
LoooooooL - avatar
+ 2
"var i in src" it's cool, I thought, it's only Python stuff 😀
27th Jun 2018, 1:23 PM
LoooooooL
LoooooooL - avatar
+ 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
27th Jun 2018, 6:39 PM
LoooooooL
LoooooooL - avatar
+ 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.
27th Jun 2018, 6:45 PM
LoooooooL
LoooooooL - avatar
+ 1
Data is gethered, but file don't saves.
27th Jun 2018, 1:36 PM
LoooooooL
LoooooooL - avatar