For further actions, you may consider blocking this person and/or reporting abuse
Visualizing Promises and Async/Await 🤯
☝️ Check out this all-time classic DEV post
The Problem With React
Stephen Belovarich -
Processing promises in Batch
Sibelius Seraphini -
Next.js 13 Image Cheatsheet
Ever wonder why React's setState doesn't update immediately?
Top comments (52)
This little snippet to sleep
I'm afraid to ask where you need this one 😂😂
I've used it for polling. And... Sometimes you got to do what you got to do 😂
Hahaha no judgements. Still I let you some polling thingy I had here that may be more suitable:
pollfunction is a higher-order function that returns a function,
executePollfunction returns a
promiseand will run recursively until the condition is met.
fn: an API request (or another async thingy that suits for this polling).
validate: Test function to see if the data matches what we want, in which case it will end the poll.
interval: To specify how much it wait between poll requests.
maxAttempts: how many tries before throwing an error.
I couldn't sleep last night and read this comment at 3am. Coincidentally, today, I needed this poll. Works great in my usEffect().
The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters.
Thanks for the code!
Didn't get the meaning of the sentence "The only thing that caught me is that the parameters to the above poll function are in a class, instead of individual parameters." still glad to see it helped you 😁
Apologies, I didn't explain correctly.
Initially, I called the poll function incorrectly as follows:
The correct method (using your code) is:
Oh! nothing to do with classes it's just it receives an object but this is opinionated as I prefer to have the object structure for those "config" thingies, you can simply delete the brackets on the function declaration like that:
and we should be good 😁
The new Promise callback should not be an async function. By passing an async fn to new Promise you’re “double promising”.
Refactoring to not depend on resolve/reject would solve that.
Having it as async lets you handle the response, the error and any action to perform either it went OK or KO, so it can be customised specifically on it's environment.
instead handling it as generic inside the polling function.
But sure you can tweak it as you wish to fullfill your needs 😄
This one is pretty neat, I also use it sometimes to "mock" API responses like this 👇
I love that one!
I've never used that one xD
Here are a few of the more generally useful ones:
Those are currently very useful ones!
I've a slightly different base64encode one
I'll check the differences later 😁
The base64 functions from @lionel-rowe are compatible with (most) browser. The fs module is only available in Node.js :)
Of course but what I want to check is the details on what's happening on this
decodemethod and also the reason for it to use an array of 8-bit unsigned integers 😂
TextDecoderconvert strings to and from their equivalent UTF-8 bytes. So for example,
new TextEncoder().encode('foo 福 🧧')gives
Uint8Array [102, 111, 111, 32, 231, 166, 143, 32, 240, 159, 167, 167], where bytes 0..2 represent the 3 ASCII characters "foo", 4..6 represent the single 3-byte character "福", and 8..11 represent the single 4-byte character "🧧" (3 and 7 are spaces).
Thanks you for the explanation! 😁
I have an informal stash of useful regexes I copy and paste as needed. Informal meaning it's not quite organized enough for me to cleanly list them here, but it's sort of a system I have that has evolved naturally.
So this kind of things that are informal till you make a repo out of it because you've used it in like 30 projects and it has been standarized in some sort across your software😂
C'mon show us some functions 🤣🤣
I'm not sure about those TypeDefs honestly, I think it's better to have an inline, let's say:
so it's more clear for everyone than saying
but to keep the honesty in this comment, this is opinionated and I may be wrong 😅
I don't really have snippets I reuse. I might occasionally pinch a function from another project, but there's nothing I can think of that would be worth a snippet, and I've never really remembered to use them when I've tried.
I do have these vim mappings for older PHP projects which don't have much in the way of development tools:
They'll let me paste in a variable dump with my cursor positioned where I need to put in the thing to debug, or a generic stack trace. They also cope with frameworks that do tricky things with output buffering.
That's about it.
😂 I was recently wondering why should I keep doing that for every new project idea, so I made a ts npm package to just import my own code everywhere.
Take a look if you want: npmjs.com/package/js-math-and-ui-u...
I work a lot with animations and free-hand user input so one often use is
I have an rng function that returns a random number between 1-x, and a randomizer function that returns a random item from an array-- the latter is probably used even more than the former!
I also set up a janky function to populate complicated dropdown menus based on arrays and nested arrays (element ID, array, dropdown type). I've ended up reusing it much more than anticipated, so should probably refactor it, ha.
We all have those
TODO: Refactor this function/methodat the bottom of our TODO list 😂
This is one I made and using across many projects.
It seems a bit overcomplicated to me, I'd do something like:
I think it does the same but... Am I missing something maybe? Probably some use case that is not in my mind 😅
For input like
Mary Jane Watson, your function returns
MJW, and his returns
It's common practice to use only two letters as initials on avatars (eu.ui-avatars.com/api/?name=Mary+J...)
That was one of my first thoughts but initials depend on the country, so my name is Joel Bonet Rxxxxx, being Joel my name and Bonet + Rxxxxx my two surnames.
So in this case I'll be JR with this approach which in my country and according to my culture or what is reasonable, it should be JB. (first character of the name + first character of the surname).
That's the main reason for getting separate fields for name and surname/s.
So your name can be "Mary Jane Linda" and your surnames can be "Lee Bonet".
and still getting ML as initials which would be the most correct output as far as I can tell.
Here I am enjoying all the great, open conversations about programming and now I learn something about other cultures too. #Winning
You must restrict your initials to some letter count either one or two, see your initial from the above fn will grow with no. of words in a name which is not favourable to be called 'initials'
And yes my country surnames is at last so picking the last one in my code 😅
Depends on the type of project.
For react websites I have a blocker (loading animation, waiter) which is 80% universal, maybe add a custom (s)css to style it.
I also keep a standard react-table setup that adds a layer upon react-table fixing the issues it has in typescript (types aren't extended when a function is added,)
rafceshortcut is a life-saver.
I have a framer motion animations file whith all pre coded I just need to import to the new project, thinking about to turn it into a Npm package, so it will be even easier to import
That's a great thing to do! (and usually pretty convenient).
I just did it yesterday with this thing here. Got 200 downloads the first day so I guess others found it useful 😅
I'll need to wait till next Sunday for the weekly stats to update
Can you share it with us? Framer motion is a great package tho
@mangor1no I have to implement it, and for sure I will, and it will be open for contributions too
I would rather build a reusable library and make it available to anyone that finds it useful.
Function for prevent using
try catchin all async await calls
and use it like this
to- funny name. I use similar helper, but I call it
go. Here my recent post about it dev.to/fedyk/golang-errors-handing...
😂 Both @c_basso and @fedyk solutions are coded in TS that can be the cause of your confusion @kevinleebuchan, here's a JS translation (guys correct me if I miss something)
Note that the return value of the
gofunction is always an array with two positions, the first one intended to be a successful result, the second one reserved for the error so in case it succeeds it sends
[result, null]and in case it fails it sends back
[null, error], this may help to understanding:
Hope it helps! 😁
let callApi=(path) => fetch(path).then(response => response.json())
This will work just for GET requests without authentication, let me do a little tweak on it by just adding an optional "options" parameter 😁
now you can perform any HTTP verb request on a new layer of functions like
PUT example (POST will look mostly the same):
You can try it in your browser terminal right now:
This is my favourite one and universal too:
The identity function in any language that doesn't have it.