Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/junosuarez/promesa

easiest way to return a promise from a function
https://github.com/junosuarez/promesa

Last synced: 8 days ago
JSON representation

easiest way to return a promise from a function

Awesome Lists containing this project

README

        

# promesa
easiest way to return a promise from a function

*"promesa" is Spanish for "promise"*

## usage
```js
var Promesa = require('promesa')

// (Number) => Promise
function getUser(id) {
return Promesa(function () {
if (!(typeof id === 'number' && Number.isFinite(id) && !Number.isNaN(id))) {
throw new TypeError('id must be a number')
}

return {id: id, name: 'Boris Yeltsin'}
})
}
```
In a real implementation, you would probably have some sort of
underlying asynchronous value, like a database lookup.

`Promesa` lets you mix and match returning Promise values,
synchronous values, and throwing. As explained in
[You're missing the point of promises][1], Promises/A+ lets
you reason about your code in terms of `return` and `throw`
semantics you already know and love.

[1]: https://gist.github.com/domenic/3889970

## FAQ

### When should I use `promesa` instead of another way of making a promise?

Promesa is ideal for making functions which are working with other promises as well as synchronous logic, for example precondition (guard) checking, authorization logic, etc. It assumes that any asynchronous values you're working with are already represented as Promises.

If you need to create promises from other kinds of asynchronous control flow patterns, such as EventEmitters or callback continuations, consider using something like `bluebird`'s `Promise` constructor.

## api

### `promesa: (fn: Function) => Promise`

Invokes the function `fn` and wraps the result in a Promise.
If `fn` returned a Promise, that value will be flattened and returned
(see the [Promise Resolution Procedure][2]). If `fn` returned a
value, a resolved Promise of that value will be returned. If `fn`
throws, a rejected Promise will be returned.

[2]: https://github.com/promises-aplus/promises-spec#the-promise-resolution-procedure

## installation

$ npm install promesa

## running the tests

From package root:

$ npm install
$ npm test

## contributors

- jden

## license

MIT. (c) MMXIII jden . See LICENSE.md