-
Notifications
You must be signed in to change notification settings - Fork 9
Quick Reference
You've come across righto and you just want to understand what it's doing quickly, This is the page for you.
An eventuals implementation (like a promise but different in important ways)
righto works with both promises AND plain old callbacks (err-backs)
righto DOES NOT CATCH THROWN ERRORS
righto lazily evaluates
If you are using err-backs, you use righto like this:
var eventual = righto(errBack, arguments...);
and you can execute the eventual like this:
eventual(function(error, result){ ... });
If any argument passed into righto is an eventual (either a righto or a promise) righto will first resolve them, and then call your function with the resolved arguments
If any argument rejects, the whole eventual is rejected.
var rightoEventual = righto(errBack, arguments...)
var rightoEventual = righto.from(promise)
righto.sync(): var object = righto.sync(JSON.parse, eventualJSON)
var rightoEventual = righto.from(anything)
var eventual = righto.sync(createPromise, args...)
##Basically Promise.then:
var bar = eventualFoo.get('bar')
async version of var bar = foo['bar']
var object = eventualJSON.get(JSON.parse)
you can return an eventual here too.
Same as promise.all(), var allInAnArray = righto.all(args...)
var result = righto(fs.readFile, fileName, 'utf8', righto.after(something))
Righto.all for objects: var resolvedObject = righto.resolve({ foo: eventualFoo })
Recover from, or change errors produced by an eventual: var handled = righto.handle(failingEventual, (error, done) => done(null, 'Default value')
mate eventuals together: result = righto.mate(a, b, c); result(function(error, a, b, c){ ... })
if an eventual resolves multiple values, take the ones you want:
function resolveTwoValues(callback){
callback(null, 'a', 'b');
}
var eventual = righto(resolveTwoValues);
var secondResult = righto.take(eventual, 1) // 0 indexed arguments
secondResult(console.log): // "null, 'b'" (no error, result: 'b')
Creates a rejected eventual, useful in .get()'s: eventual.get(value => value || righto.fail('Not found!'))
resolve an array of [error?, result?] that will be either [error] OR [null, result]
Assume file1 contains the text file2.txt
- eventuals from err-backs (fs.readFile)
- sequential
var eventualFile1 = righto(fs.readFile, 'file1.txt', 'utf8');
var eventualFile2 = righto(fs.readFile, eventualFile1, 'utf8'); // <- The result of eventualFile1 is used as an argument to the second readFile.
// Get the result
eventualFile2(function(error, result){ ... });
Because eventualFile2
needs eventualFile1
before it can run, it automatically waits for it to complete.
Here we have a synchronous function concat
that just returns a value, so we use righto.sync
instead
- eventuals from err-backs (fs.readFile)
- parallel
function concat(string1, string2){
return string1 + string2;
}
var eventualFile1 = righto(fs.readFile, 'file1.txt', 'utf8');
var eventualFile2 = righto(fs.readFile, 'file2.txt', 'utf8');
var eventualConcattedFiles = righto.sync(concat, eventualFile1, eventualFile2);
// Get the result
eventualConcattedFiles(function(error, result){ ... });
Both files will be loaded in parallel automatically.
I'll stop calling everything eventual{Thing}
here since that's only useful when learning the basics.
Say you want to run a task that you dont need the result for, for example, sending an email:
- eventuals from err-backs (
function getUserById(userId, callback){ ... }
) - eventuals from promises (
function getProductById(productId){ ... return productPromise; }
) - parallel loading of user and product
- waiting for a task to complete before executing another ( sent email after purchased )
- Creation of a desired result after awaiting multiple tasks (
righto.mate(purchased, righto.after(emailSent))
)
function buyProduct(userId, productId, callback){
var user = righto(getUserById, userId);
var productPromise = getProductById(productId); // Returns a Promise eventual.
var purchased = righto(makePurchase, user, productPromise);
// We want to wait for `purchased` to complete before sending the confirmation email.
// but `sendPurchaseConfirmation` only takes 2 arguments.
// righto.after(eventual) arguments are waited for, but not passed as an argument.
var emailSent = righto(sendPurchaseConfirmation, user, productPromise, righto.after(purchased));
// We want to wait for the email to be sent before responding, but we want to respond with the result of
// the purchase. `righto.mate` combines eventuals and can be used with `righto.after` to both
// wait for a successful email, and then resolve `purchased`
var result = righto.mate(purchased, righto.after(emailSent));
// Execute the result, passing in our callback.
result(callback);
}