kulmapaikka-ircbot/plugins/helpers/requestPromise.js
2018-10-27 14:50:31 +03:00

35 lines
722 B
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var request = require('request');
var Promise = require('promise');
function RequestPromise(url, urlArgs, headers, json) {
if(typeof headers === 'undefined') {
headers = {};
}
if(typeof json === 'undefined') {
json = false;
}
var promise = new Promise(function(resolve, reject) {
request({
url: url,
qs: urlArgs,
headers: headers,
json: json
}, function(error, response, body) {
if(!error && response.statusCode == 200) {
if (json === false) {
resolve(JSON.parse(body)); /// wtf
} else {
resolve(body)
}
} else {
reject(error);
}
});
});
return promise;
}
module.exports = RequestPromise;