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

132
Views
Return value from callback

I have value inside callback function and couldn't return successfully.

async pos(convertedCodeDBSearch) {
    var dd;
    const x =  await idealPostcodes.lookupAddress(convertedCodeDBSearch, function (error, searchResults)  {
      return searchResults.result.hits[1];
    });
    console.log('x', x);
  }

I need to return searchResults.result.hits[1]; from function. Here is the code, Thank you.

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

0

I imagine you are using this npm package or a fork of it. If this is the case, you should revise your logic, because the call to lookupAddress does not return a value by itself, meaning that the x in your code will always be undefined.

Since you are already working with async functions, my suggestion is using the Node helper util.promisify to extract the result from the callback function into a promise. This promise can be awaited to get the success value of the original callback (here searchResults).

const { promisify } = require('util');

async pos(convertedCodeDBSearch) {
    var dd;
    const searchResults = await promisify(idealPostcodes.lookupAddress.bind(idealPostcodes))(convertedCodeDBSearch);
    return searchResults.result.hits[1];
}

about 4 years ago · Juan Pablo Isaza Report

0

Not sure if it'll solve you're problem, but why don't you get rid of the callback entirely and utilize the async/await syntax to it's fullest?

async pos(convertedCodeDBSearch) {
    var dd;
    try {
        const x =  await idealPostcodes.lookupAddress(convertedCodeDBSearch);
        const results = x.result.hits[1];
        console.log(`The results are: ${results}`);
        return results;
    } catch (e) {
        console.error("Something went wrong")
    }
}
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!