Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/junosuarez/promesa
- Owner: junosuarez
- License: mit
- Created: 2013-09-20T20:37:35.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2013-12-06T02:34:05.000Z (almost 11 years ago)
- Last Synced: 2024-10-31T17:20:07.788Z (14 days ago)
- Language: JavaScript
- Size: 117 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
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