Content
@
0 reply
0 recast
0 reaction
Samuel ツ
@samuellhuber.eth
Using Typescript do you prefer your libraries to return promises or something else? with network requests to e.g. hubs on farcaster we can't get instant returns of data. Appreciate feedback on the best way to do it. Here's a proposal by @nazeeh
5 replies
1 recast
6 reactions
jtgi
@jtgi
Not sure I get the question. If it’s an async call, your choice is a promise or a callback. You should choose promise in 2024. It’s composable. Callbacks aren’t. From the image it seems like Nazeeh is comparing promises vs async/await, but they’re both promises, latter is just syntactic sugar.
1 reply
0 recast
1 reaction
Samuel ツ
@samuellhuber.eth
Yeah one could do await in the library function for you and try to get it to be a call like let value = sum(1,2); Where sum(a, b) = a + b; So it’s atomic?! Or just have dev do let value = await sum(a, b); Or sum(a, b).then( (c) => value = c;) It’s about what is the best way to return for a library function
2 replies
0 recast
0 reaction
Samuel ツ
@samuellhuber.eth
Possibly even observers? I am mostly unfamiliar with either and just now I forget the await for async calls A LOT! that’s why I am asking for wisdom of /frontend what is best and what people love https://warpcast.com/pjc/0x26681619
3 replies
0 recast
0 reaction
Phil Cockfield
@pjc
Ah, you probably want Promises (Hot), not Observerables (Cold). Observerables are flexible structures for handling streams of events, but in the case you're probably working in, Promises are the most native thing. As @jtgi points to, async/await is just synatic sugar around the underlying data structure of Promise<T>
1 reply
0 recast
0 reaction
jtgi
@jtgi
I recommend reading this: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises Also, open up a repl in node and play with sample implementations. You’ll see. Good luck!
1 reply
0 recast
0 reaction
jtgi
@jtgi
You don’t need to choose between async await and promises as a library author. They both return promises, the caller decides. Also, if you forget to await the compiler will complain in almost all scenarios. It’s not a factor.
0 reply
0 recast
0 reaction