iriscouch / browser-request

Browser library compatible with Node.js request package
Apache License 2.0
360 stars 102 forks source link

Simple way to make a request sync? #75

Open 01AutoMonkey opened 7 years ago

01AutoMonkey commented 7 years ago

By default browser-request is async but I don't see any obvious way to make it sync. I have two functions, getHTTP and HTTPExists, but I also want to make getHTTPSync and HTTPExistsSync.

What are my options to make them sync?

function HTTPExists(url, cb) {
  request({ url: resolve(url), method: 'HEAD' }, function(err, res) {
    if (err) return cb(null, false);
    cb(null, /4\d\d/.test(res.statusCode) === false);
  });
}
function getHTTP(url, cb) {
  request(resolve(url), function (error, response, body) {
    if (!error && response.statusCode == 200) {
      cb(null, body);
    } else {
      cb(error, body)
    }
  })
}
nadavye commented 6 years ago

I think that the current API doesn't support it. Seems like it requires a minor change to the code cause as far as I was able to tell, seems like they create async requests without a way to configure it.

If you find a way around it, please drop me a line as I need that too.