I’m still working on wasteof.money for snap(.berkeley), but because I know how to use the wasteof api, I have came to the realization that I now have to create a mod with custom blocks (which i am very skilled at)
The .then statements will only run when the fetch is finished. This is very likely not done when res is being returned, so it’s still "undefined" . Does Snap handle promises? If it does, you can either use async/await (preferred) or just return the promise without the res part. If not, then you can’t really use fetch in a Snap block.
DreamBerd's true purpose is to make fun of people who make fun of JavaScript. Just look at this abomination:
Just look at this abomination.
typeof NaN === "number"
Yeah, you read that right. JavaScript thinks that 'not a number' is a 'number'. You probably didn't know this.
Yeah, you read that right. They think that 'not a number' is 'not a number'. But 'not a number' is a 'number' as part of the IEFFFF spec. You probably didn't know this.
That's the kind of high level satire you can expect from DreamBerd.
Except... That's not the truth. Here's the real truth:
You’re returning a promise there, that’s told to get the text when its done. You’d have to put the alert inside the then statement, so it only gets called when the promise is actually done. There’s a lot of callback hell involved with dealing with promises as they are. async/await solves a lot of that. I’d recommend reading more about promises to understand what they are and how they work: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
i am not getting any console error, nor the test alert i have put in, which is making me think previous js code is making it not work. here is my entire html doc, can you tell me what’s wrong?
You're not getting any errors at all? That's strange. I'd suggest removing the window.onload thing (as the script is at the bottom of the page anyways). Does that do anything?
The
.thenstatements will only run when the fetch is finished. This is very likely not done whenresis being returned, so it’s still"undefined". Does Snap handle promises? If it does, you can either useasync/await(preferred) or just return the promise without therespart. If not, then you can’t really usefetchin a Snap block.This ^
yet another reason to not use javascript
async/awaitmakes this a lot betteryeah, I'm just saying JavaScript is a mess (said by toadpond)
:)))))
thats a nice opinion.
except for the fact its literally programmed in javascript
true
Yes
ok
wait how
But
async/awaitis still the better option.ok
It’s returning [object Promise], can you send me some code with async/await?
async/await would do the same thing, it's just syntactic sugar for promises. Snap doesn't seem to support promises, then.
halp
You’re returning a promise there, that’s told to get the text when its done. You’d have to put the
alertinside the then statement, so it only gets called when the promise is actually done. There’s a lot of callback hell involved with dealing with promises as they are.async/awaitsolves a lot of that. I’d recommend reading more about promises to understand what they are and how they work: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promisestysm
wait rq can you give me an example of using async/await with this?
And the function it's wrapped in has to be async, e.g.
i tried
and it isnt working, am i doing something wrong?
The brackets are unmatched, but apart from that, that should work. What's the error you're getting?
i am not getting any console error, nor the test alert i have put in, which is making me think previous js code is making it not work. here is my entire html doc, can you tell me what’s wrong?
<!doctype HTML>
<head>
</head>
<html>
<body>
<script src="https://twemoji.maxcdn.com/v/latest/twemoji.min.js" crossorigin="anonymous"></script>
<script>
window.onload = function() {
// Parses the document body and
// inserts <img> tags in place of Unicode Emojis
twemoji.parse(document.body, {
folder: 'svg',
ext: '.svg'
} // This is to specify to Twemoji to use SVGs and not PNGs
);
}
</script>
<style>
img.emoji {
width: 1.5em;
pointer-events: none;
}
</style>
<script>
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"username": "mrowlsss",
"password": "pass123donthackme"
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
alert("a");
const token = await (await fetch("https://api.wasteof.money/session", requestOptions)).text;
alert(token);
</script>
</body>
</html>
You're not getting any errors at all? That's strange. I'd suggest removing the
window.onloadthing (as the script is at the bottom of the page anyways). Does that do anything?actually, better and easier option, can you just give me code that will alert the response of the fetch?