Skip to content

Commit

Permalink
improved promise polyfill usage under node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Hovhannes Babayan committed Dec 26, 2014
1 parent 5e1138d commit 6be94b6
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 70 deletions.
97 changes: 52 additions & 45 deletions dist/attask.js

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions examples/node/create-new-project.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
global.Promise = require('promise');

var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

Expand Down
2 changes: 0 additions & 2 deletions examples/node/get-project-details.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
global.Promise = require('promise');

var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

Expand Down
2 changes: 0 additions & 2 deletions examples/node/login.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
global.Promise = require('promise');

var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

Expand Down
2 changes: 0 additions & 2 deletions examples/node/logout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
global.Promise = require('promise');

var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

Expand Down
2 changes: 0 additions & 2 deletions examples/node/search-for-projects.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
global.Promise = require('promise');

var ApiFactory = require('./../../').ApiFactory;
var util = require('util');

Expand Down
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ gulp.task('build', ['clean-build'], function() {
standalone: 'AtTask'
}
)
.ignore('promise/polyfill')
.bundle()
.pipe(source('attask.js'))
.pipe(gulp.dest(BUILD_DIR));
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('promise/polyfill');

var ApiFactory = require('./src/ApiFactory');
var Api = require('./src/Api');
var ApiUtil = require('./src/ApiUtil');
Expand Down
24 changes: 9 additions & 15 deletions src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,10 @@ module.exports = function(Api) {
options.path = path_module.join(this.httpOptions.path, path);

if (fields.length !== 0) {
params["fields"] = fields.join();
params['fields'] = fields.join();
}
options.path += '?' + queryString.stringify(params);

// Add xsrf header
//var xsrfValue = urlIsSameOrigin(config.url)
// ? cookies.read(config.xsrfCookieName || defaults.xsrfCookieName)
// : undefined;
//if (xsrfValue) {
// headers[config.xsrfHeaderName || defaults.xsrfHeaderName] = xsrfValue;
//}

return new Promise(function (resolve, reject) {
/*options = {
hostname: 'echo.jsontest.com',
Expand All @@ -36,13 +28,15 @@ module.exports = function(Api) {
withCredentials: false
};*/

var req = http.request(options, function (res) {
var request = http.request(options, function (response) {
var body = '';
//res.setEncoding('utf8');
res.on('data', function (chunk) {
if (typeof response.setEncoding === 'function') {
response.setEncoding('utf8');
}
response.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
response.on('end', function () {
var data = JSON.parse(body);
if (data.error) {
reject(data);
Expand All @@ -51,8 +45,8 @@ module.exports = function(Api) {
}
});
});
req.on('error', reject);
req.end();
request.on('error', reject);
request.end();
});
};
};
Expand Down

0 comments on commit 6be94b6

Please sign in to comment.