Promise chaining vs async/await

Oh man, in my React/Redux final project, which uses New York City’s open data to create a dashboard for city renters, I ended up having to chain a lot of API GET requests together because of the way the data was organized.

For instance, to get a building’s Registration Info (information about building owner and manager), I had to first lookup a building’s Building Identification Number (BIN) in one dataset, cross-reference than BIN with a Registration ID in another, and use the Registration ID in a third to finally come across the Registration Info.

And all that resulted in (assume fetchBin, fetchRegId, and fetchRegInfo are the appropriate API calls:

export function getRegistrationInfo(address) {
  return dispatch => {
    dispatch({ type: 'GET_BIN', address });

    return fetchBin()
      .then(
        response => {
          dispatch({ type: 'GOT_BIN', address, response });
          dispatch({ type: 'GET_REG_ID', address });

          return fetchRegId()
        }
      )
      .then(
        response => {
          dispatch({ type: 'GOT_REG_ID', address, response });
          dispatch({ type: 'GET_REG_INFO', address });

          return fetchRegInfo()
        }
      .then(
        response => {
          dispatch({ type: 'GOT_REG_INFO , address, response});
        }
      )
  }
}

I just don’t like it. Maybe some people like chaining these Promises because it’s like a timeline or something, but I think it just looks bad!

Enter async and await. Unfortunately I didn’t know about them until my code review, but I wish I had! Introduced in ES2017, it’s a new way to handle asynchronous functions in JavaScript!

async allows you to declare a function that will return a Promise. More importantly, async functions can contain await expressions that pauses execution while the expression is resolved. Using async/await, I could refactor the above code as follows:

export async function getRegistrationInfo(address) {
  return dispatch => {
    dispatch({ type: 'GET_BIN', address });
    const binResponse = await fetchBin();
    dispatch({ type: 'GOT_BIN', address, binResponse });

    dispatch({ type: 'GET_REG_ID, address });
    const regIdResponse = await fetchRegId();
    dispatch({ type: 'GOT_REG_ID', address, regIdResponse });

    dispatch({ type: 'GET_REG_INFO, address });
    const regInfoResponse = await fetchRegInfo();
    dispatch({ type: 'GOT_REG_INFO, address, regInfoResponse });
  }
}

To me, that looks so much better and simpler. I understand there are some difficulties with error catching when using async/await, which I’ll have to look more into. And I admit that my Promise chaining did not look as neat as the example I have above, which isn’t that bad, because I hadn’t seen the proper way to do it, as detailed here. I had been indenting with every then, which was just getting crazy…

Anyway, at least it’s another option I now know about!

What’s going on with 18F and The U.S. Digital Service?

While writing my last blog post about tech-for-good and civic tech, I was reminded about how much I used to talk about the 18F and U.S. Digital Services programs of the Federal government. They were founded during the Obama administration to bring modern technologies and processes to Washington.

I wondered: what has happened to them over the last two years? Are they still thriving and innovating under a much different regime?

Wait, 18 what and the U.S. Digital who??

18F

18F (named for the intersection—18th St and F St—in Washington, DC where they’re headquartered) is a “an office of federal employees within the General Services Administration (GSA) that collaborates with other agencies to fix technical problems, build products, and improve how government serves the public through technology.”

They partner with other agencies in the federal government to build digital services utilizing lean startup practices, they are dogmatic about developing with transparency (open-source), and they aim to leave their partner agencies with a sense of how software development looks outside the massive bureaucracy of the federal government.

Some of their earliest work includes:

  • https://analytics.usa.gov/, which displays Google Analytics data for many government websites. In case you’re interested, the government website with the most users at the moment I’m writing this is the USPS’s tracking page. By far.
  • https://collegescorecard.ed.gov/, a website which allows users to compare the cost and value of colleges throughout the United States.
  • https://myra.gov/, a website for the Treasury Department’s myRA program, which was a government-sponsored Roth IRA account.

U.S. Digital Service

The U.S. Digital Service is actually within the Executive Office of the President—the “White House”—so it has a more top-down approach. It recruits top technologists for short-term stints in the government to work on the Administration’s top priorities. It was created after th HealthCare.gov rollout disaster to prevent something like it from ever happening again.

Some of their earliest work:

  • https://my.uscis.gov/ (in partnership with 18F), a website for helping users navigate through the immigration process.
  • https://www.va.gov/, a website for Veterans to discover, apply for, track, and manage their benefits.
  • IRS Secure Access, which allows taxpayers to access their tax transcripts online, rather than through the mail.

Changes under Trump

With Trump coming into office, many were worried that these programs’ futures were grim at best. And while there certainly have been changes and large reductions in staff—at least at 18F—both still exist and are doing good work! The U.S. Digital Service is actually still run by an Obama-era hire!

18F

According to Fast Company, 18F’s staff headcount has shrunk by at least half since Trump’s inauguration, and many staff resigned in protest of Trump’s policies and personal behavior. However, there are still civil servants there hard at work, making sure the good work started under Obama continues. In fact, just this month, they released the second version of their design guidelines for new government websites—the United States Web Design System 2.0.

U.S. Digital Service

Because the U.S. Digital Service is focused on advancing the priorities of the White House, I am more surprised to find that it is still operational, and even more surprised that it’s being run by Matt Cutts, who was there during the Obama administration. And it looks like I’m not alone in that thinking, with numerous articles referencing it (just search “matt cutts trump”). It appears the common response is that their crew is actually nonpartisan, working for the “American People.”

And they have done some great things, like saving the VA $100 million by streamlining their cloud infrastructure processes, and helping to build anti-drone defenses. And right now, they’re working on upgrading Medicare’s payment system, which is apparently 40 years old and written in COBOL and assembly.

But it’s weird to see that they’re working on helping immigrants and refugees, when that’s clearly anathema to the White House. I hope they can continue to do that kind of work without Mr. Trump ever finding out about it!