Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
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?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I don't know what API you're using but you can try BluetoothRemoteGATTCharacteristic API. It has writeValueWithResponse method which return Promise.

https://developer.mozilla.org/en-US/docs/Web/API/BluetoothRemoteGATTCharacteristic

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

If I understood you correctly then you can do it like this

const sendData = async (device, data) => {
  const response = await device.write(data);
  await Promise.all(device.responseListeners.map(listener => listener(response)))
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!