request / request-promise

The simplified HTTP request client 'request' with Promise support. Powered by Bluebird.
ISC License
4.76k stars 297 forks source link

[Query] Synchronous function using request-promise ?? #286

Closed jay11ca39 closed 5 years ago

jay11ca39 commented 6 years ago

I want to write a synchronous function, which return the rest response which it receive from server. For example:

function get(address) {                          ------->  address = http://www.google.com
    rp(address)
        .then(function (htmlString) {
            // Process html...
        })
        .catch(function (err) {
            // Crawling failed...
        });
}

The above function should exit only when it receive response from the server.... I am not sure about the working of request-promise will it provide this feature or there is any other way to do this ?

triadium commented 5 years ago

No. You cannot write synchronous function with promises. But you can use an "await/async" chain.

analog-nico commented 5 years ago

Like @triadium said, you cannot do this. The requests are based on node.js’s core libraries which are async by design. But indeed, await / async are your friends. Or generators (function *) and yield for older versions of node.js.