Browse Source

fetch: always return a Promise

Even when JS fetch function is missing.
Herby Vojčík 4 years ago
parent
commit
dfb603e96a
4 changed files with 36 additions and 56 deletions
  1. 12 22
      lang/src/Platform-Browser.js
  2. 6 6
      lang/src/Platform-Browser.st
  3. 12 22
      lang/src/Platform-Node.js
  4. 6 6
      lang/src/Platform-Node.st

+ 12 - 22
lang/src/Platform-Browser.js

@@ -21,13 +21,11 @@ var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $early={};
-try {
-$recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
+return $recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-throw $early=[$recv(fetch)._value_(aStringOrObject)];
+return $recv(fetch)._value_(aStringOrObject);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({fetch:fetch},$ctx1,1)});
 //>>excludeEnd("ctx");
@@ -35,25 +33,22 @@ throw $early=[$recv(fetch)._value_(aStringOrObject)];
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-return $self._error_("fetch not available.");
+return $recv($globals.Promise)._signal_("fetch not available.");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
 //>>excludeEnd("ctx");
 }));
-return self;
-}
-catch(e) {if(e===$early)return e[0]; throw e}
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"fetch:",{aStringOrObject:aStringOrObject})});
 //>>excludeEnd("ctx");
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aStringOrObject"],
-source: "fetch: aStringOrObject\x0a\x09self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | ^ fetch value: aStringOrObject ]\x0a\x09\x09ifAbsent: [ self error: 'fetch not available.' ]",
-referencedClasses: [],
+source: "fetch: aStringOrObject\x0a\x09^ self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | fetch value: aStringOrObject ]\x0a\x09\x09ifAbsent: [ Promise signal: 'fetch not available.' ]",
+referencedClasses: ["Promise"],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:", "error:"]
+messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:", "signal:"]
 }),
 $globals.BrowserPlatform);
 
@@ -66,13 +61,11 @@ var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $early={};
-try {
-$recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
+return $recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-throw $early=[$recv(fetch)._value_value_(aString,anObject)];
+return $recv(fetch)._value_value_(aString,anObject);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({fetch:fetch},$ctx1,1)});
 //>>excludeEnd("ctx");
@@ -80,25 +73,22 @@ throw $early=[$recv(fetch)._value_value_(aString,anObject)];
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-return $self._error_("fetch not available.");
+return $recv($globals.Promise)._signal_("fetch not available.");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
 //>>excludeEnd("ctx");
 }));
-return self;
-}
-catch(e) {if(e===$early)return e[0]; throw e}
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"fetchUrl:options:",{aString:aString,anObject:anObject})});
 //>>excludeEnd("ctx");
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aString", "anObject"],
-source: "fetchUrl: aString options: anObject\x0a\x09self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | ^ fetch value: aString value: anObject ]\x0a\x09\x09ifAbsent: [ self error: 'fetch not available.' ]",
-referencedClasses: [],
+source: "fetchUrl: aString options: anObject\x0a\x09^ self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | fetch value: aString value: anObject ]\x0a\x09\x09ifAbsent: [ Promise signal: 'fetch not available.' ]",
+referencedClasses: ["Promise"],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:value:", "error:"]
+messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:value:", "signal:"]
 }),
 $globals.BrowserPlatform);
 

+ 6 - 6
lang/src/Platform-Browser.st

@@ -15,15 +15,15 @@ globals
 !BrowserPlatform methodsFor: 'public API'!
 
 fetch: aStringOrObject
-	self globals at: #fetch
-		ifPresent: [ :fetch | ^ fetch value: aStringOrObject ]
-		ifAbsent: [ self error: 'fetch not available.' ]
+	^ self globals at: #fetch
+		ifPresent: [ :fetch | fetch value: aStringOrObject ]
+		ifAbsent: [ Promise signal: 'fetch not available.' ]
 !
 
 fetchUrl: aString options: anObject
-	self globals at: #fetch
-		ifPresent: [ :fetch | ^ fetch value: aString value: anObject ]
-		ifAbsent: [ self error: 'fetch not available.' ]
+	^ self globals at: #fetch
+		ifPresent: [ :fetch | fetch value: aString value: anObject ]
+		ifAbsent: [ Promise signal: 'fetch not available.' ]
 !
 
 newXhr

+ 12 - 22
lang/src/Platform-Node.js

@@ -21,13 +21,11 @@ var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $early={};
-try {
-$recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
+return $recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-throw $early=[$recv(fetch)._value_(aStringOrObject)];
+return $recv(fetch)._value_(aStringOrObject);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({fetch:fetch},$ctx1,1)});
 //>>excludeEnd("ctx");
@@ -35,25 +33,22 @@ throw $early=[$recv(fetch)._value_(aStringOrObject)];
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-return $self._error_("fetch not available.");
+return $recv($globals.Promise)._signal_("fetch not available.");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
 //>>excludeEnd("ctx");
 }));
-return self;
-}
-catch(e) {if(e===$early)return e[0]; throw e}
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"fetch:",{aStringOrObject:aStringOrObject})});
 //>>excludeEnd("ctx");
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aStringOrObject"],
-source: "fetch: aStringOrObject\x0a\x09self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | ^ fetch value: aStringOrObject ]\x0a\x09\x09ifAbsent: [ self error: 'fetch not available.' ]",
-referencedClasses: [],
+source: "fetch: aStringOrObject\x0a\x09^ self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | fetch value: aStringOrObject ]\x0a\x09\x09ifAbsent: [ Promise signal: 'fetch not available.' ]",
+referencedClasses: ["Promise"],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:", "error:"]
+messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:", "signal:"]
 }),
 $globals.NodePlatform);
 
@@ -66,13 +61,11 @@ var self=this,$self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-var $early={};
-try {
-$recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
+return $recv($self._globals())._at_ifPresent_ifAbsent_("fetch",(function(fetch){
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-throw $early=[$recv(fetch)._value_value_(aString,anObject)];
+return $recv(fetch)._value_value_(aString,anObject);
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({fetch:fetch},$ctx1,1)});
 //>>excludeEnd("ctx");
@@ -80,25 +73,22 @@ throw $early=[$recv(fetch)._value_value_(aString,anObject)];
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx2) {
 //>>excludeEnd("ctx");
-return $self._error_("fetch not available.");
+return $recv($globals.Promise)._signal_("fetch not available.");
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx2) {$ctx2.fillBlock({},$ctx1,2)});
 //>>excludeEnd("ctx");
 }));
-return self;
-}
-catch(e) {if(e===$early)return e[0]; throw e}
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 }, function($ctx1) {$ctx1.fill(self,"fetchUrl:options:",{aString:aString,anObject:anObject})});
 //>>excludeEnd("ctx");
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aString", "anObject"],
-source: "fetchUrl: aString options: anObject\x0a\x09self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | ^ fetch value: aString value: anObject ]\x0a\x09\x09ifAbsent: [ self error: 'fetch not available.' ]",
-referencedClasses: [],
+source: "fetchUrl: aString options: anObject\x0a\x09^ self globals at: #fetch\x0a\x09\x09ifPresent: [ :fetch | fetch value: aString value: anObject ]\x0a\x09\x09ifAbsent: [ Promise signal: 'fetch not available.' ]",
+referencedClasses: ["Promise"],
 //>>excludeEnd("ide");
 pragmas: [],
-messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:value:", "error:"]
+messageSends: ["at:ifPresent:ifAbsent:", "globals", "value:value:", "signal:"]
 }),
 $globals.NodePlatform);
 

+ 6 - 6
lang/src/Platform-Node.st

@@ -15,15 +15,15 @@ globals
 !NodePlatform methodsFor: 'public API'!
 
 fetch: aStringOrObject
-	self globals at: #fetch
-		ifPresent: [ :fetch | ^ fetch value: aStringOrObject ]
-		ifAbsent: [ self error: 'fetch not available.' ]
+	^ self globals at: #fetch
+		ifPresent: [ :fetch | fetch value: aStringOrObject ]
+		ifAbsent: [ Promise signal: 'fetch not available.' ]
 !
 
 fetchUrl: aString options: anObject
-	self globals at: #fetch
-		ifPresent: [ :fetch | ^ fetch value: aString value: anObject ]
-		ifAbsent: [ self error: 'fetch not available.' ]
+	^ self globals at: #fetch
+		ifPresent: [ :fetch | fetch value: aString value: anObject ]
+		ifAbsent: [ Promise signal: 'fetch not available.' ]
 !
 
 newXhr