Make a promise-based function return a destructurifiable object.
Uses the native promise implementation.
npm install destructure-promise
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' }
}
})();
MIT