Skip to content

JordanSinko/destructure-promise

Repository files navigation

destructure-promise

Travis branch Sonarcloud Status LGTM Quality LGTM Alerts Sonarcloud Coverage NPM Downloads NPM License

Make a promise-based function return a destructurifiable object.

Uses the native promise implementation.

Installation

npm install destructure-promise

API

destructure-promise(fn)

Takes a promise-based function and returns the destructuried function. The idea for this was from Go's-like version of a tuple response.

const destructure = require('destructure-promise');

(async () => {
  // Resolved values
  {
    const task = arg => Promise.resolve(arg);
    const destructured = destructure(task);
    const [, response] = await destructured('Hello world'); // 'Hello world'
  }
  // Resolved values
  {
    const task = () => Promise.reject({ message: 'Some error' });
    const destructured = destructure(task);
    const [error] = await destructured(); // { message: 'Some error' }
  }
})();

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published