ƒ
This is NOT Promise library. This a a small module that makes working with ES6 Promises a bit easier and/or more semantic.
FYI:
The ƒ
character is ALT+f
on the Mac.
The Ø
character is ALT+SHIFT+o
on the Mac.
npm install effd
var ƒ = ;var fs = ƒ; fs; fs;
API
ƒ(function)
Creates a promise. Similar to new Promise((resolve,reject)=>{})
The callback function
will be passed 1 argument... Ø
Ø
argument...
About that It's a Swiss Army knife argument. As mentioned above, it is a lambda that converts error first callback style to Promise's (resolve,reject)
style.
Ø.resolve = Ø.done = Ø.accept = Ø.ok
Aliases to resolve
the promise.
Ø.reject = Ø.error = Ø.fail = Ø.no
Aliases to reject
the promise.
Ø.error is special! It will create an instance of an Error if it isn't given one.
;//true
ƒ(value|Error)
Returns a rejected Promise
if it is an instance of Error
, otherwise a resolved Promise
.
Similar to doing Promise.resolve(value)
or Promise.reject(Error)
.
ƒ(property, modifier)
Shortcut for ƒ.filter.
ƒ.promisify(context)
ƒ.promisify(context, name)
ƒ.promisify(context, names)
ƒ.promisify(context, ...names)
ƒ.promisify(fn)Turns an error first callback style function into a thenable.
ƒ.promisify(fn)
can only be used if the function does not need it's parent's context, otherwise you will need to
pass the parent and the function's name:
var users_collection = ƒ name: 'Node' var fs = ƒ;fs; //or you can just promisify ALL functions...// (even ones that don't make sense!)var fs = ƒ;fs;;
then & ƒ.catch
ƒ.A getter that returns a resolved Promise.
ƒ.catch
also returns a resolved Promise- therefor its callback will never get called. Its only purpose is to provide a consistent interface.
var promise = ƒ;promise;promise;
resolve = ƒ.done = ƒ.accept = ƒ.ok = ƒ.noop = ƒ.echo
ƒ.Aliases for Promise.resolve()
. Use whichever reads the best to you!
reject = ƒ.error = ƒ.fail = ƒ.no
ƒ.Aliases for Promise.reject()
. Use whichever reads the best to you!
ƒ.error is special! It will create an instance of an Error if it isn't given one.
ƒ;//true
race(Array)
ƒ.Alias of Promise.race.
all(Array)
ƒ.Alias of Promise.all.
ƒ.all(...args)
Converts the arguments
to an array and then uses Promise.all(Array)
ƒ.all(Object)
Passes the values of the Object
to Promise.all(Array) then maps the results to an Object
matching the keys of the original Object
.
ƒ.filter(property, modifier)
Creates a then
filter (a function that can be passed to a .then()
function) that will modify a property of the
object it is given. After modifying the source object, it will return it to the .then()
function. If the modifier
returns a thenable, it will return that promise to .then()
.
property • The property to modify.
modifier • A function that takes in the original value and returns the new value or a thenable for async operations.
//syncusers ; //asyncvar //start a chain
ƒ.passthrough(function) / ƒ.passthrough(context, name)
Turns a function that isn't async into a resolved promise. Since lambdas allow us to drop the {}
s and return
statement if everything is chained, you can use this to help simplify your code.
var log = ƒ;var ; var ; ;
Here's another example:
var debug = ƒ;moduleexports = //use a lambda to get access to the value being passedthrough! ;
In this example, the debug
passthrough is used in 2 ways. The first, creates a resolved Promise that is thenable. There isn't a value to passthrough, so there isn't a value to pass to the first then()
. The second is passed the user
object returned from the database
Here's an example...
modulesexports = { return { somedb; }; }
Well, ƒ all those extra characters crowding things!!
var ƒ = ;modulesexports = { return ; }
Hmm… converting that callback style is still pretty ugly! That Ø
argument is a converter too! Check it out...
var ƒ = ;modulesexports = { return ; }
Better, but... Gaaahh! This is really just a proxy to db.find
. Lets just Promisify it...
var ƒ = ;modulesexports = find: ƒ
Oops... Doing it like that screws up its scope. We need to pass in the db
context and then function name to fix it:
var ƒ = ;modulesexports = find: ƒ
👍
license
MIT © 2015 William Kapke