DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

K (he/him)
K (he/him)

Posted on

Proxymise Your Async Code

If you've written a fair amount of JavaScript these days, it's likely that you came across promises and their language integration async functions. With Proxymise there is now a way to make your code even more consise!

What

Proxymise is a JavaScript library that lets you wrap functions that return promises so you can call methods on the objects they would resolve to.

Why

When using synchronous functions, you can use the returned value directly. This lets you write fluent interfaces for your libraries.

select("*").from("myTable").where("x > 10");
Enter fullscreen mode Exit fullscreen mode

While async functions helped a bit with some types of invocations, it still can clutter your code.

Proxymise wants to solve this issue.

How

Instead of writing a then chain like this:

fetch("example.com")
.then(r => r.json())
.then(r => r.record.id)
.then(id => ...);
Enter fullscreen mode Exit fullscreen mode

Or a await list like that:

let r = await fetch("example.com");
r = await r.json();
const {id} = r.record;
...
Enter fullscreen mode Exit fullscreen mode

It allows you to wrap your promise and call methods on it, making the your interfaces more fluent.

const newFetch = proxymise(fetch);

const {id} = await newFetch("example.com").json().record;
Enter fullscreen mode Exit fullscreen mode

It does this by returning proxy objects right away while the promise is still in flight. These will then wait for the resolve and apply the function calls later.

Conclusion

Proxymise is a quick way to make asynchronous library usage more consise by getting rid of boilerplate code.

Top comments (9)

Collapse
 
bias profile image
Tobias Nickel

Today I hear the first time about proxy objects, now I find this articel, solving an awefull problem that always disgust me.

Thanks a lot ! !

Collapse
 
kayis profile image
K (he/him) • Edited on

Cool :D

I read about proxies rather often, but this is the first application that really blew my mind :D

Collapse
 
kozhevnikov profile image
Ilya Kozhevnikov

Thanks! Btw it's 'proxymise' rather than 'proximise' (portmanteau of 'proxy' and 'promise' that was still available on npm).

Thread Thread
 
kayis profile image
K (he/him)

Oh, good point! :D

Fixed it!

Collapse
 
asparallel profile image
AsParallel

What does the error handling construct look like for proxymise? I know I can read the docs, but that's not very conversational. Im assuming proxymise(op, err) or .catch.

Collapse
 
kayis profile image
K (he/him) • Edited on
try {
  const {id} = await newFetch("example.com").json().record;
}
catch(e) {
  console.log(e.message);
}
Collapse
 
nektro profile image
Meghan (she/her)

Wow! What an amazing idea! πŸ‘πŸ‘

Collapse
 
d0ruk profile image
Doruk Kutlu

Genius.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

11 Tips That Make You a Better Typescript Programmer

typescript

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!