examples: Update fetch example

This commit is contained in:
Remko Tronçon 2022-05-14 07:47:04 +02:00
parent 5cbbd733ff
commit eab641d0d1
3 changed files with 20 additions and 20 deletions

View file

@ -2767,7 +2767,7 @@
(func (export "tos") (result i32)
(global.get $tos))
(func (export "here" (result i32))
(func (export "here") (result i32)
(global.get $here))
(func (export "interpret") (result i32)

View file

@ -13,34 +13,34 @@ import WAForth from "waforth";
await forth.load();
// Bind "age" call to a function that fetches the age of the given person, and calls the continuation callback
forth.bind("age", async (stack) => {
const name = stack.popString();
const result = await (
await fetch("https://api.agify.io/?name=" + encodeURIComponent(name))
).json();
// After this point, use the `forth` object directly, since we're no longer in the callback.
forth.push(parseInt(result.age));
forth.interpret("AGE-CB");
forth.bind("?ip", async () => {
const cbxt = forth.pop();
try {
const result = await (
await fetch("https://api.ipify.org?format=json")
).json();
forth.pushString(result.ip);
forth.push(cbxt);
forth.interpret("EXECUTE");
} catch (e) {
console.error(e);
}
});
// Load Forth code to bind the "age" call, and define the continuation callback
forth.interpret(`
: AGE ( c-addr u -- )
S" age" SCALL
: ?IP-CB ( c-addr n -- )
." Your IP address is " TYPE CR
;
: AGE-CB ( d -- )
." Your age is " .
;
: GUESS-AGE ( -- )
S" Remko" AGE
: ?IP ( -- )
['] ?IP-CB
S" ?ip" SCALL
;
`);
// Ask for a number (via Forth) when the user clicks the button
btn.addEventListener("click", () => {
forth.interpret("GUESS-AGE");
forth.interpret("?IP");
});
})();

View file

@ -34,7 +34,7 @@ import WAForth from "waforth";
( Prompt the user for a number, and write it to output )
: ASK-NUMBER ( -- )
S" Please enter a number" PROMPT
." The number was" SPACE .
." The number was " . CR
;
`);