pas2js/demo/asyncawait/tryfetch.lpr
2020-05-28 08:10:50 +00:00

35 lines
697 B
ObjectPascal

program tryfetch;
{$mode objfpc}
uses
browserconsole, JS, Web, SysUtils;
procedure myFetch; async;
var
response: TJSResponse;
myBlob: TJSBlob;
image: TJSHTMLImageElement;
objectURL: string;
begin
try
response := await(window.fetch('pas2js.png'));
if not response.ok then
raise Exception.Create('HTTP error! status: '+str(response.status))
else begin
myBlob := await(response.blob());
objectURL := TJSURL.createObjectURL(myBlob);
image := TJSHTMLImageElement(document.createElement('img'));
image.src := objectURL;
document.body.appendChild(image);
end;
except
console.log(JSExceptValue);
end;
end;
begin
myFetch;
end.