Hacker News new | ask | show | jobs
by franciscop 2225 days ago
I do Javascript and wish throwing quick scripts was a bit easier, which are made a bit harder with promises/async since now you have to separate your scripts and functions depending on their color. So I made a library to help me[1]:

    const name = await swear(fetch('/some.json')).json().user.name;
    console.log(name);  // Francisco

    const error = await swear(readFile('./error.log')).split('\n').pop();
    console.log(error);  // *latest error log message*
It makes all functions to look like blue functions, but internally they are all red. I made this by using `Proxy()`[2], then queuing the operations and waiting for any unfinished one on the last operation, which is always a `.then()` (since there's an `await`). It is fully compatible with native promises.

While I do not use it directly since adding a library to make syntax slightly shorter defeats the point, I've included it into some of my async libraries:

• File handler `files`: https://www.npmjs.com/package/files

• Simple command runner `atocha`: https://www.npmjs.com/package/atocha

• Enhanced fetch() `fch`: https://www.npmjs.com/package/fch

[1] https://www.npmjs.com/package/swear

[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...