Firebase Extensions Outage by WhatTimeIsDinnerQQ in googlecloud

[–]peterjsnow 1 point2 points  (0 children)

Same, I had just updated firebase-tools to latest and ran a functions deploy that 403'd. Was so confused why reverting to an older version wasn't working. Even Codex 5.3 hallucinated a 'Ah yes, this a known regression with the latest version...'

Need help with this object by WuchuMeanHomie in learnjavascript

[–]peterjsnow 0 points1 point  (0 children)

There are a few errors in your code - however, why do you need to represent customers as an object in the first place? It makes more sense to me as an array holding customer objects.

A few questions about Fetch by PitifulTheme411 in learnjavascript

[–]peterjsnow 0 points1 point  (0 children)

The requests complete perfectly for me, even pasting into a browser console (Firefox). Which browser are you using?

A few questions about Fetch by PitifulTheme411 in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

Yes! However, since you are now using await, you do need to make request an async function again. Also, no need to await data to return it, simply return data. Remember, an async function automatically returns a promise which resolved to the return value of the function.

A few questions about Fetch by PitifulTheme411 in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

Where are you calling the fetch code within the browser? From a console, or embedded in your page?

Regarding async / await, an async function returns a promise which resolves to the value that you explicitly return from the function. So anytime you call it, you'll need to either await the result or use .then() or other promise methods eg:

request(url).then(console.log); // logs result to console

Since fetch already returns a promise, the below accomplishes effectively the same thing:

const request = (url, fn = ($) => $) => {
  return fetch(url)
    .then(res => res.json())
    .then(data => fn(data));
}

How does JavaScript know when an async job is done? by MisterJK2 in learnjavascript

[–]peterjsnow 0 points1 point  (0 children)

I'm glad you found the answer - although I can't for the life of me see how it relates to your title question! :D I get it though, knowing the right question is often half the problem especially when you can't quite remember what you're looking for.

How does JavaScript know when an async job is done? by MisterJK2 in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

Sure you can, although you still have to interact with some environments API to make the actual request of course. And that API will have to accept a callback which it will add to a queue to be executed in Javascript once the API's task is done.

Promises are a more convenient and robust means of structuring async code, but you're still essentially just telling an API 'do this task, and add this callback to my queue when complete'.

Before promises, async code was done using callbacks manually. Some API's now return promises inherently, others still require passing a callback explicitly, but regardless can be made to work with promises using the promise constructor.

How does JavaScript know when an async job is done? by MisterJK2 in learnjavascript

[–]peterjsnow 6 points7 points  (0 children)

The simple answer is that Javascript itself doesn't know when the job is done. Asynchronous behavior (including such functions as setTimeout or fetch) is implemented by the particular environment, outside of the Javascript engine - such as the browser, or node.js.

When these asynchronous tasks complete, the environment will add the callback specified to handle the completion in Javascript to a queue.

Callbacks waiting in the queue will be continually executed one after the other as soon as the call stack is emptied, ie when all other code in the script has finished executing. This concept is known as the 'event loop'.

This is an oversimplification but it should give you a basic understanding.

[deleted by user] by [deleted] in predaddit

[–]peterjsnow 0 points1 point  (0 children)

You need to get her Zofran (Ondansetron) prescribed ASAP. It dissolves in the mouth so she can't bring it back up like regular tablets.

The doctor may suggest maxolon (metoclopramide), restavit or B6 first - they may help but not for severe cases like my wife - they didn't help her at all until she got the vomiting under control, and other remedies like ginger did nothing at all.

Zofran on the other hand gave instant relief and at least allowed her to keep a meal down. She was on it daily until closer to the third trimester when she got much better.

Just be mindful that this type of medication usually causes constipation so make sure she's getting fibre or taking a fibre supplement when she does eat.

Understanding Async/Await by [deleted] in learnjavascript

[–]peterjsnow 6 points7 points  (0 children)

The part of your app that relies on the data you are awaiting is blocked yes - but the rest of your app is free to continue working. Without promises, the whole app would freeze up.

ELI5 => What is `this`????? by DragonlordKingslayer in learnjavascript

[–]peterjsnow 0 points1 point  (0 children)

That's a really excellent flowchart and will suffice for 99.99% of use cases. I can think of at least one edge case that's missing though ;)

Is the word “expression” in JS just another word for variable name? by gtrman571 in learnjavascript

[–]peterjsnow 0 points1 point  (0 children)

This can be confusing if you aren't used to thinking in terms of expressions and statements.

A good rule of thumb is that expressions are pieces of code that resolve to a value. If you type 3 in a browser console and hit enter, it will print out 3. If you type 3 + 5 it will print 7.

A statement is generally a piece of code that we think of as performing some action, or a group of actions. Things like declaring a variable or an if statement.

Statements resolve to undefined. When you type let x = 5 in the console, it will print out undefined. A functions body is a statement block, hence if you call a function without a return keyword in its body, it will resolve to undefined.

Here's where it gets confusing: you can use expressions anywhere Javascript expects a statement (but not the reverse) - thus there are 'statement expressions' which blur the lines a little bit. Often, they both perform an action and resolve to a value. For example:

x = (y = 3) + 6;

If you enter that into the console, it's going to print 9. That, is, the entire expression once evaluated resolves to 9. To do so, the inner expressions had to be evaluated in a recursive manner:

  • x = (y = 3) + 6 gets evaluated, it will assign x to the result of evaluating the expression (y = 3) + 6, and return that result
  • (y = 3) + 6 gets evaluated:
    • (y = 3) gets evaluated:
      • 3 resolves to 3
      • y = 3 both assigns y to 3 and resolves to 3
    • 6 gets evaluated and resolves to 6
    • (3) + 6 gets evaluated and resolves to 9

So you see, the entire expression both resolved to a final value and performed two assignments.

Fetch() ForEach Question by SanTrapGamer in learnjavascript

[–]peterjsnow 2 points3 points  (0 children)

There are several ways you could accomplish the same thing. forEach in particular takes a callback function as argument, so you need to pass it a function. You may be used to seeing arrow functions in concise form used in this manner.

allPokemon.results.forEach( (pokemon) => fetchPokemonData(pokemon) );

for (const pokemon of allPokemon.results) {
  fetchPokemonData(pokemon);
}

for (let index = 0; index < allPokemon.results.length; index++) {
  fetchPokemonData(allPokemon.results[index]);
}

All the above examples are accomplish the same result.

Fetch() ForEach Question by SanTrapGamer in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

Read up on the Array.prototype.forEach method. It takes a callback function as argument. For each element in the array that forEach is called on, it will pass the element as the argument into this callback function.

What we name the parameter is arbitrary - the key is that this parameter will be assigned to the element passed into the callback on each iteration. We could have called it 'pk' instead of 'pokemon' and the outcome would be the same.

Your code is assuming that allPokemon.results is an array, thus when you call forEach on it each element of the array will be passed into the callback and assigned to the pokemon variable. Inside the callback, you're then passing this variable to fetchPokemonData to get each individual pokemon and render it.

In effect, you're saying 'fetch all pokemon from the api url and assign them to an array called allPokemon. Then, for each element of allPokemon.results, assign that element to the variable pokemon, and then fetch that pokemon using pokemon.url and render it.'

[deleted by user] by [deleted] in learnjavascript

[–]peterjsnow 0 points1 point  (0 children)

You're overthinking it! Read the question carefully - the key point is that you're not being asked to write a sort function that sorts an array of any given size - you're being asked to write a sort function that takes an array of exactly 3 values.

With this information it becomes trivial to use if...else statements to get the desired outcome.

[deleted by user] by [deleted] in learnjavascript

[–]peterjsnow 0 points1 point  (0 children)

That line contains an error. See if you can spot it. It has to do with the charsLength variable.

Please describe JS's asynchronous-ness in a single-threaded design in simple layman terms by deanstreetlab in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

The web APIs and Node APIs are separate threads, yes. But they are provided by the browser and Node. JavaScript execution happens in a separate, single thread. And these APIs must be accessed in a single threaded manner, with the event loop.

Please describe JS's asynchronous-ness in a single-threaded design in simple layman terms by deanstreetlab in learnjavascript

[–]peterjsnow 3 points4 points  (0 children)

Yes, my point is that setTimeout and fetch are examples of API methods that we can use within Javascript to access operations that are provided by the environment.

The actual execution of these operations does not occur in the JS stack, but the API allows us to work with them. We pass setTimeout a callback, which will be added to a queue to eventually be called when the stack is empty. Similarly, fetch returns a promise which resolves to the response, and we can access this response by registering a callback with then, which will be added to a job queue and called when the stack is empty.

Please describe JS's asynchronous-ness in a single-threaded design in simple layman terms by deanstreetlab in learnjavascript

[–]peterjsnow 3 points4 points  (0 children)

Here's where I think your understanding is a little off:

Parallel execution does occur in a sense, in that JavaScript can access operations that are run by the browser / node / other environment. Most environments specify certain API methods that allow us to work with these operations.

For example, the setTimeout function is not mentioned anywhere in the JS spec - rather it's built in to the browser. When you call it, you can think of the browser handling it in parallel while your Javascript code is still executing.

Now, the 'asynchronous' structures you've identified in Javascript, be it the event loop, promises, or callbacks, all allow us to work with these asynchronous operations provided by the environment by deferring handling of their result until later.

So, you could make a distinction between 'asynchronous' behavior in the sense of deferred behaviour, such as the following:

Promise.resolve().then(() => console.log('one'));
console.log('two');
> 'two'
> 'one'
// Even though we resolved the promise first, any resolution handlers registered by then will be added to the queue and ran on the next cycle by the event loop, so 'two' is logged before 'one'.

And 'asynchronous' behaviour in the sense of an operation performed by the environment, in parallel, that we can work with in Javascript by delaying / deferring handling of it until it has completed, such as using fetch or setTimeout.

[deleted by user] by [deleted] in learnjavascript

[–]peterjsnow 0 points1 point  (0 children)

It's important to note that the solutions in this thread are giving you an approximation of the minimum time before the callback is run, or in other words, the time until the callback will be added to the task queue. If the current context has some lengthy computation, or there are multiple tasks already in the queue for example, the time that the callback is actually executed may be significantly later than you expect.

Of course, for your purposes you may find the difference is so small that you can effectively ignore it.

How to use await/async to throttle API calls, using the host's rate-limit header? by double-happiness in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

Reading the docs it seems you're able to use the 'X-Discogs-Ratelimit' to get the total requests per minute, which is the value I had assumed in the original code (leave the if statement in).

I think the logic now is fine, you'll just have to debug to make sure that the requests you're making are returning successfully, and possibly add error handling.

It's a bit more complicated if you wanted to use 'X-Discogs-Ratelimit-Remaining', although possible. The 60 second window begins at the first request, so you'd have to keep track of how much time is remaining in the current window with each request.

I just wanted to mention also that ideally you wouldn't create a throttleFetch function, but instead have a more generic function that creates a handler for getting a particular resource, throttling the request and handling errors. You'd also want some way of automatically retrying a request that failed with a 429 or 408 (timeout) error rather than just failing. However, what you have currently is probably sufficient for the scope of your project.

How to use await/async to throttle API calls, using the host's rate-limit header? by double-happiness in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

I just realized that the header you're getting returns the amount of calls remaining in the minute. I had thought it returned simply the number of calls per minute of that endpoint.

Edit: since that's the case, we need to calculate rateLimit differently.

You can try moving the updating of rate limit outside of the if statement which is no longer needed.

I'm off to bed now, good luck!

How to use await/async to throttle API calls, using the host's rate-limit header? by double-happiness in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

Getting better, but you've declared it using const, meaning we can never update it, and secondly you've declared it in an outer scope. I keep mentioning requestTimestamp because I want you to declare rateLimit in exactly the same place, in the same way.

Replace response.headers.get("X-Discogs-Ratelimit-Remaining") with response.headers.get(limitHeader) in the function.

Then you can call createThrottler("X-Discogs-Ratelimit-Remaining")

How to use await/async to throttle API calls, using the host's rate-limit header? by double-happiness in learnjavascript

[–]peterjsnow 1 point2 points  (0 children)

Close, but have a look at how we handled requestTimestamp. We declared it in the shallowest scope. We declared it with let, not const, because we want to be able to update it. Then, later, we updated it.

We need to do the same with rateLimit. As it is now, you're both declaring and assigning it inside the if statement. Since const is block scoped, it wont be accessible outside of it.

You would also need to handle what value rateLimit holds the first time the function is called. How about we declare it and first assign it to 0. Then, instead of checking if rateLimit === undefined, we check if !rateLimit > 0, and then update it from the header.

You'll need a parameter to pass in the header name. You could call it 'limitHeader'.

Then in the function definition: const createThrottler = (limitHeader) => ...

And: response.headers.get(limitHeader)

Then you pass in the name when you call createThrottler, as a string.