DEV Community

loading...

Discussion on: Describe the Best Interview You've Been In

Collapse
antjanus profile image
Antonin Januska

I had an interview a few years back and it's still stuck in my mind. It was an interview where I learned something. Even if the job didn't work out (and it didn't), I ended up wiser afterward.

I did a live coding session using one of those apps where people can collaborate and I was asked to implement a couple of cool features using promises.

I think the feature is called multiplexing but essentially, I had to come up with a small function/library that allowed a multitude of callers to ask for the same URL/resource and instead of firing off a bunch of requests to the same URL. I believe my solution was to create an object like this:

let pendingPromises = {
  'posts/all': {
    promise, //actual promise that I used to ask for the data from server
    awaiting: [], // array of callers that asked for `posts/all` before it returns
  }
};

The other features had to do with sharing a single URL but ask for separate resources. Eg. you'd ask for posts/all but at the same time, you also ask for users/all and let's say posts/1. The challenge was to combine the several URLs in a single tick, and then ask the server for all those resources. I didn't create a full implementation for this one but basically, I combined the above feature to gather all URLs and their callers and then did something like this:

fetch(`/api/multiplex`, {
  body: JSON.stringify({
    resources: Object.keys(pendingPromises)
  })
})

And explained that I'd use the server's router to figure out which resources to fetch, do a Promise.all on them, and return an object like so:

{
  'posts/all': { data: [] },
  'users/all': { data: [] },
  'posts/1': { data: [] },
}

I still think about that interview and how much it taught me!