node package manager



easiest way to return a promise from a function

"promesa" is Spanish for "promise"


var Promesa = require('promesa')
// (Number) => Promise<User> 
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, Promises/A+ lets you reason about your code in terms of return and throw semantics you already know and love.


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.


promesa: (fn: Function<T>) => Promise<T>

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). If fn returned a value, a resolved Promise of that value will be returned. If fn throws, a rejected Promise will be returned.


$ npm install promesa

running the tests

From package root:

$ npm install
$ npm test



MIT. (c) MMXIII jden See