Promise/Future Resolution

We need to be able to feed API request responses back to our application. This is a little bit of a hassle when dealing with WebSockets since requests return responses through the socket.onmessage method. It's very possible to do though with a little massaging.

Let's set up some state variables on our Socket Service.

let openRequests = [];
let pairingPromise = null;

  • openRequests will be used to hold promises used to resolve requests.
  • pairingPromise will be used to hold a pairing promise. We only need one of these. You could easily merge this into the openRequests as well instead of creating it's own variable, but this keeps things separated.