Get Promise resolve from separate callback

I am sending data to a Bluetooth device, and the responses are handled by a listener that’s set up during the connection process:

device.connect().then(device => {
  device.registerResponseListener((data) => {
    // handle response
  }
}

I have a separate function that sends data to the device:

const sendData = (device, data) => {
  device.write(data);
}

My question is, how can I Promisify this code? I’d like to be able to do

const sendData = (device, data) => {
  return new Promise((resolve, reject) => {
    device.write(data);
    // resolve...?
  });
}

But how do I get the resolve into the Bluetooth response listener?

Answer

The best possible solution in this case, while still not ideal, was to store the resolve function in variable at a higher scope:

var sendDataResolve;

device.connect().then(device => {
  device.registerResponseListener((data) => {
    sendDataResolve(data);
  }
}

const sendData = (device, data) => {
  return new Promise((resolve, reject) => {
    sendDataResolve = resolve;
    device.write(data);
  });
}

...

sendData(device, "data")
.then(result => {
  console.log("Got result",result);
});

The caveat is that Promise resolutions are NOT guaranteed to be tied correctly to the original request. This ONLY works with one request at a time.