0

I want to get multiple async urls, parse them and return value for continue code.

const Promise = require("bluebird");
const rp = require('request-promise');

var request1 = rp('https://www.wp.pl');
var request2 = rp('https://www.google.pl');
var request3 = rp('https://www.dell.com/support/home/pl/pl/pldhs1/product-support/servicetag/41H3D5J/diagnose');
//var request4 = ......

var output = Promise.all([request1, request2, request3])
    .spread(function (responseOfReq1, responseOfReq2, responseOfReq3) {
        console.re.log(responseOfReq1.split("\n")[0]);
        console.re.log(responseOfReq2.split("\n")[0]);
        console.re.log(responseOfReq3.split("\n")[2]);

        //parse result and return data

        return 'aaaaa'; //return data;
    })
    .catch(function (err) {
        if(err!='')
        console.re.error(err);
    });

console.re.info('done get urls');
console.re.debug(output);

console return responseOfReq1, responseOfReq2, responseOfReq3 but execution code locked on Promise, and console.re.info('done get urls'); doesn't do.

Where is problem?

Peter
  • 499
  • 10
  • 30
  • @estus This is example for javascript, not for node. And my syntax is different from Your link – Peter Oct 21 '18 at 10:54
  • What do you mean by code locked on Promise? – Baboo Oct 21 '18 at 11:06
  • @Baboo_ code in promise is excecuted, but after promise, `console.re.info('done get urls');` doesn't show, this mean that executing code don't go to this line. I know thah is `resovle` promise, but I don't know how use this in my code. – Peter Oct 21 '18 at 11:09
  • 2
    Node isn't a different language. It's a platform that uses Javascript. Answers cover all possible asynchronous calls, including promises and totally applicable to Node. You just need to read them. TL;DR: you cannot synchronously return a result to a variable from asynchronous request. Because it's **a**synchronous. `console` should reside inside `then`. – Estus Flask Oct 21 '18 at 11:12
  • @estus this mean, that I cat run asynchronus process from main program, but I can't get result asynchronus proces in main program? You say `then`... Hmmm, maybe I can run asynchronus program and in `then` parse data and send data to browser – Peter Oct 21 '18 at 11:18
  • @estus there is an asynchronous handling problem for sure but it doesn't explain why `done get urls` is not displayed – Baboo Oct 21 '18 at 11:21
  • @Peter have you tried using `console.log` instead of `console.re.info`? – Baboo Oct 21 '18 at 11:23
  • *Hmmm, maybe I can run asynchronus program and in then parse data and send data to browser* - yes, that's how it's done. It's unknown what's `console.re` in your case. If it's custom console implementation then blame it on it. – Estus Flask Oct 21 '18 at 11:33
  • @estus Yes, this is Console.re, because I have node on MyDevil.net server and node is running as process. When I run node from console, I have a lot of errors – Peter Oct 21 '18 at 11:46
  • You don't seem to understand that `Promise.all()` returns a promise. The ONLY way to get the value out of that promise is to use `await` or `.then()` with that promise. To use an asynchronously obtained value you have to use asynchronous ways of programming. There are no other ways around it. – jfriend00 Oct 21 '18 at 15:44
  • I must read about `await` and `.then()` method. I want in multiple intances get data from url (about 20 urls), and after get data from all request, parse them, and continue execute main program – Peter Oct 21 '18 at 20:25
  • `console.re.info('done get urls');` will "do" ***before*** all those `responseOfReq#` – Jaromanda X Oct 22 '18 at 03:08
  • @JaromandaX Yes. Ok now I know that I must parse result in `.spread`. But I know what is what – Peter Oct 22 '18 at 10:18

0 Answers0