@@ -769,6 +769,9 @@ p5.prototype.loadBytes = function(file, callback, errorCallback) {
769
769
* @param {function } [errorCallback] function to be executed if
770
770
* there is an error, response is passed
771
771
* 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.
772
775
* @example
773
776
* <div class='norender'><code>
774
777
* // Examples use USGS Earthquake API:
@@ -808,19 +811,21 @@ p5.prototype.loadBytes = function(file, callback, errorCallback) {
808
811
* @param {Object|Boolean } data
809
812
* @param {function } [callback]
810
813
* @param {function } [errorCallback]
814
+ * @return {Promise }
811
815
*/
812
816
/**
813
817
* @method httpGet
814
818
* @param {String } path
815
819
* @param {function } callback
816
820
* @param {function } [errorCallback]
821
+ * @return {Promise }
817
822
*/
818
823
p5 . prototype . httpGet = function ( ) {
819
824
p5 . _validateParameters ( 'httpGet' , arguments ) ;
820
825
821
826
var args = Array . prototype . slice . call ( arguments ) ;
822
827
args . splice ( 1 , 0 , 'GET' ) ;
823
- p5 . prototype . httpDo . apply ( this , args ) ;
828
+ return p5 . prototype . httpDo . apply ( this , args ) ;
824
829
} ;
825
830
826
831
/**
@@ -839,6 +844,9 @@ p5.prototype.httpGet = function() {
839
844
* @param {function } [errorCallback] function to be executed if
840
845
* there is an error, response is passed
841
846
* 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.
842
850
*
843
851
* @example
844
852
* <div>
@@ -908,19 +916,21 @@ p5.prototype.httpGet = function() {
908
916
* @param {Object|Boolean } data
909
917
* @param {function } [callback]
910
918
* @param {function } [errorCallback]
919
+ * @return {Promise }
911
920
*/
912
921
/**
913
922
* @method httpPost
914
923
* @param {String } path
915
924
* @param {function } callback
916
925
* @param {function } [errorCallback]
926
+ * @return {Promise }
917
927
*/
918
928
p5 . prototype . httpPost = function ( ) {
919
929
p5 . _validateParameters ( 'httpPost' , arguments ) ;
920
930
921
931
var args = Array . prototype . slice . call ( arguments ) ;
922
932
args . splice ( 1 , 0 , 'POST' ) ;
923
- p5 . prototype . httpDo . apply ( this , args ) ;
933
+ return p5 . prototype . httpDo . apply ( this , args ) ;
924
934
} ;
925
935
926
936
/**
@@ -942,7 +952,9 @@ p5.prototype.httpPost = function() {
942
952
* @param {function } [errorCallback] function to be executed if
943
953
* there is an error, response is passed
944
954
* 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.
946
958
*
947
959
* @example
948
960
* <div>
@@ -999,6 +1011,7 @@ p5.prototype.httpPost = function() {
999
1011
* <a href="https://developer.mozilla.org/en/docs/Web/API/Fetch_API">reference</a>
1000
1012
* @param {function } [callback]
1001
1013
* @param {function } [errorCallback]
1014
+ * @return {Promise }
1002
1015
*/
1003
1016
p5 . prototype . httpDo = function ( ) {
1004
1017
var type ;
@@ -1091,35 +1104,41 @@ p5.prototype.httpDo = function() {
1091
1104
}
1092
1105
}
1093
1106
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
+ }
1102
1120
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 ;
1123
1142
} ;
1124
1143
1125
1144
/**
0 commit comments