promise.flatten

1.0.0 • Public • Published

promiseFlatten

Resolves a promise with [response, error] instead of using a try/catch block

npm install promise.flatten

Why?

With async and await now we can have our asynchronous code cake and eat it too. But as complexity grows we can still find ourselves in try/catch hell:

  try {
    await workItHarder();
  } catch (e) {
    return await makeItBetter();
  }

  doIt.makeUs(['harder', 'better', 'faster', 'stronger']);

  try {
    await everAfterWork.isOver();
  } catch(e) {
    console.log('enough of this');
  }

We find ourselves with a lot of cruft. Maybe we're doing something clever and don't care about the error. Here's what this looks like:

import promiseFlatten from 'promise.flatten';

const [response, error] = await promiseFlatten(fetch('https://google.com/'));

// Fun with errors
const [, error] = await promiseFlatten(fireAndForget());

// Fughettaboutit
const [response] = await promiseFlatten(feedMeResponses());

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i promise.flatten

      Weekly Downloads

      3

      Version

      1.0.0

      License

      MIT

      Unpacked Size

      2.5 kB

      Total Files

      4

      Last publish

      Collaborators

      • cpt