trina
trina is creative alternative to handle asynchronous code in JavaScript by treating Promises as booleans.
Why?
trina removes the complexity of dealing with Promise rejections and those annoying unhandled promise rejections by turning your code into asynchronous logic expressions. trina makes your code logically pure and error-proof.
How?
Let a, b and c be functions that return true or false.
let true;let false;let true; && || //-> true! || && //-> false
Now let a, b and c be functions that return a Promise which either resolves (true) or rejects (false).
let Promise;let Promise;let Promise; null //->resolves (true) with "i love trina"null //->rejects (false) with "i"
## Getting started
### Installing
npm install trina
;
To use in the browser simply include trina.js as the source file. Node.js related functions will not work. Only 'print' and 'sleep' can be used in the browser.
Note
By default trina assigns a number of functions such as trina, AND, OR, NOT, etc to the global variable. Make sure to get rid of any confliciting variable names.
trina accepts a series of functions that must be seperated by 'AND' or 'OR'. A function may return a Promise. If the Promise resolves it's treated as true whereas a Promise rejection is treated as false. The value resolved or rejected by a Promise is passed as an arugument to the next function (similar to function composition).
/*Value passed to the first function:*/'Trina';
Keep in mind that everything is 100% asynchronous. The second function after 'AND' is invoked after the first Promise has resolved.
When invoked, trina returns a function that accepts an argument and returns a Promise, hence it can be used as a parameter.
'tracy'//resolves with 'tracy loves hanging out with trina.';
Besides Promises, functions may return anything else. A truthy value (any value which is not false, 0, "", null, undefined and NaN) is treated as true whereas a falsy value is treated as false. You can use trina to solve any problem.
1//resolves to 'Trina is blonde';
To simulate negation simply use 'NOT'. A resolved Promise will act as though it rejected and vice-versa.
'tracy'//resolves to 'tracy does not like josh.';
Improtant
Both JavaScript and trina evaluate their expressions in a left-to-right fashion so you may expect the following to return false:
console; //->true
To work as expected the above statement must include parenthesis:
console; //->false
With trina you don't have to worry about parenthesis or blocks:
null//will print 'rejected';
Simple Error handling
Debugging
The print function helps in debugging. When called without any parameters, print logs the returned/resolved/rejected value of the previous function and returns that same value. Note that if the value passed to print is falsy, even if the value came from a resolved Promise, print will return a rejected Promise with the value.
'tracy'//rejectes with undefined //will log: hey tracy, bye, undefined
If you pass a string to print, print will act as before but instead log that string to the console.
sleep
The sleep function is nothing more than syntatic sugar for the infamous setTimeout. Simply call sleep with the number of seconds to sleep. sleep returns the value passed to it like print.
//resolves with 'hey trina'
writeJSON
writeJSON saves a JavaScript oject to a JSON file asynchronously. writeJSON resolves with the JavaScript object passed to it or rejects with an error if an error occures.
readJSON
readJSON reads a JSON file asynchronously and resolves with a parsed JSON file. If an error occures readJSON rejects with the error.
deleteFile
deleteFile accepts the location of a file and deletes it. deleteFile resolves with the value passed to it and rejects with an error if an error occured while deleting.
//if successful deletion prints: 'hello'
exists
exists checks if a file exists simply by passing the location of a file. If a file exists, exists resolves with the value passed to it. If a file doesn't exist, exists rejects with the value passed to it. If some other error occured, exists rejects with the error.
download
The download function downloads a file from a webserver asynchronously and saves it in a location of your choice. download resolves with the path of the file and rejects with an error if an error occured.
//downloads an html page to a file called page.html in the app directory //downloads the image in a file called microsoft.jpg in the app directory //downloads the image in in a file called microsoft.jpg in some other directory
Handling events
Events may be handled using the on function as demonstrated in the following example:
const EventEmitter = ;const event = ; //the promise is resolved or rejected only once //emits an event (message) every 3 secondsconst emitMsg = ; /*output:5im only called once555..
If the 'on' function is passed an object which is not of type EventEmitter, on rejects with the passed object.
To really understand how 'on' works it's best to copy the above example and try it locally.