Skip to content

Commit 5215106

Browse files
authored
Merge pull request #2949 from meiamsome/feature/http-promises
Add support for a Promise being returned from httpDo, httpGet and httpPost
2 parents 342d0f4 + 89b9213 commit 5215106

File tree

2 files changed

+74
-31
lines changed

2 files changed

+74
-31
lines changed

src/io/files.js

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,9 @@ p5.prototype.loadBytes = function(file, callback, errorCallback) {
769769
* @param {function} [errorCallback] function to be executed if
770770
* there is an error, response is passed
771771
* in as first argument
772+
* @return {Promise} A promise that resolves with the data when the operation
773+
* completes successfully or rejects with the error after
774+
* one occurs.
772775
* @example
773776
* <div class='norender'><code>
774777
* // Examples use USGS Earthquake API:
@@ -808,19 +811,21 @@ p5.prototype.loadBytes = function(file, callback, errorCallback) {
808811
* @param {Object|Boolean} data
809812
* @param {function} [callback]
810813
* @param {function} [errorCallback]
814+
* @return {Promise}
811815
*/
812816
/**
813817
* @method httpGet
814818
* @param {String} path
815819
* @param {function} callback
816820
* @param {function} [errorCallback]
821+
* @return {Promise}
817822
*/
818823
p5.prototype.httpGet = function() {
819824
p5._validateParameters('httpGet', arguments);
820825

821826
var args = Array.prototype.slice.call(arguments);
822827
args.splice(1, 0, 'GET');
823-
p5.prototype.httpDo.apply(this, args);
828+
return p5.prototype.httpDo.apply(this, args);
824829
};
825830

826831
/**
@@ -839,6 +844,9 @@ p5.prototype.httpGet = function() {
839844
* @param {function} [errorCallback] function to be executed if
840845
* there is an error, response is passed
841846
* in as first argument
847+
* @return {Promise} A promise that resolves with the data when the operation
848+
* completes successfully or rejects with the error after
849+
* one occurs.
842850
*
843851
* @example
844852
* <div>
@@ -908,19 +916,21 @@ p5.prototype.httpGet = function() {
908916
* @param {Object|Boolean} data
909917
* @param {function} [callback]
910918
* @param {function} [errorCallback]
919+
* @return {Promise}
911920
*/
912921
/**
913922
* @method httpPost
914923
* @param {String} path
915924
* @param {function} callback
916925
* @param {function} [errorCallback]
926+
* @return {Promise}
917927
*/
918928
p5.prototype.httpPost = function() {
919929
p5._validateParameters('httpPost', arguments);
920930

921931
var args = Array.prototype.slice.call(arguments);
922932
args.splice(1, 0, 'POST');
923-
p5.prototype.httpDo.apply(this, args);
933+
return p5.prototype.httpDo.apply(this, args);
924934
};
925935

926936
/**
@@ -942,7 +952,9 @@ p5.prototype.httpPost = function() {
942952
* @param {function} [errorCallback] function to be executed if
943953
* there is an error, response is passed
944954
* in as first argument
945-
*
955+
* @return {Promise} A promise that resolves with the data when the operation
956+
* completes successfully or rejects with the error after
957+
* one occurs.
946958
*
947959
* @example
948960
* <div>
@@ -999,6 +1011,7 @@ p5.prototype.httpPost = function() {
9991011
* <a href="https://developer.mozilla.org/en/docs/Web/API/Fetch_API">reference</a>
10001012
* @param {function} [callback]
10011013
* @param {function} [errorCallback]
1014+
* @return {Promise}
10021015
*/
10031016
p5.prototype.httpDo = function() {
10041017
var type;
@@ -1091,35 +1104,41 @@ p5.prototype.httpDo = function() {
10911104
}
10921105
}
10931106

1094-
(type === 'jsonp' ? fetchJsonp(path, jsonpOptions) : fetch(request))
1095-
.then(function(res) {
1096-
if (!res.ok) {
1097-
var err = new Error(res.body);
1098-
err.status = res.status;
1099-
err.ok = false;
1100-
throw err;
1101-
}
1107+
var promise;
1108+
if (type === 'jsonp') {
1109+
promise = fetchJsonp(path, jsonpOptions);
1110+
} else {
1111+
promise = fetch(request);
1112+
}
1113+
promise = promise.then(function(res) {
1114+
if (!res.ok) {
1115+
var err = new Error(res.body);
1116+
err.status = res.status;
1117+
err.ok = false;
1118+
throw err;
1119+
}
11021120

1103-
switch (type) {
1104-
case 'json':
1105-
case 'jsonp':
1106-
return res.json();
1107-
case 'binary':
1108-
return res.blob();
1109-
case 'arrayBuffer':
1110-
return res.arrayBuffer();
1111-
case 'xml':
1112-
return res.text().then(function(text) {
1113-
var parser = new DOMParser();
1114-
var xml = parser.parseFromString(text, 'text/xml');
1115-
return parseXML(xml.documentElement);
1116-
});
1117-
default:
1118-
return res.text();
1119-
}
1120-
})
1121-
.then(callback || function() {})
1122-
.catch(errorCallback || console.error);
1121+
switch (type) {
1122+
case 'json':
1123+
case 'jsonp':
1124+
return res.json();
1125+
case 'binary':
1126+
return res.blob();
1127+
case 'arrayBuffer':
1128+
return res.arrayBuffer();
1129+
case 'xml':
1130+
return res.text().then(function(text) {
1131+
var parser = new DOMParser();
1132+
var xml = parser.parseFromString(text, 'text/xml');
1133+
return parseXML(xml.documentElement);
1134+
});
1135+
default:
1136+
return res.text();
1137+
}
1138+
});
1139+
promise.then(callback || function() {});
1140+
promise.catch(errorCallback || console.error);
1141+
return promise;
11231142
};
11241143

11251144
/**

test/unit/io/files_input.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,30 @@ suite('Files', function() {
7575
assert.equal(err.status, 404, 'Error status is 404');
7676
});
7777
});
78+
79+
test('should return a promise', function() {
80+
var promise = myp5.httpDo('unit/assets/sentences.txt');
81+
assert.instanceOf(promise, Promise);
82+
return promise.then(function(data) {
83+
assert.ok(data);
84+
assert.isString(data);
85+
});
86+
});
87+
88+
test('should return a promise that rejects on error', function() {
89+
return new Promise(function(resolve, reject) {
90+
var promise = myp5.httpDo('404file');
91+
assert.instanceOf(promise, Promise);
92+
promise.then(function(data) {
93+
reject(new Error('promise resolved.'));
94+
});
95+
resolve(
96+
promise.catch(function(error) {
97+
assert.instanceOf(error, Error);
98+
})
99+
);
100+
});
101+
});
78102
});
79103

80104
// tests while preload is true without callbacks

0 commit comments

Comments
 (0)