Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Promise-based operations in addition to callbacks #4

Open
ilyazub opened this issue Jan 29, 2021 · 2 comments
Open

Support Promise-based operations in addition to callbacks #4

ilyazub opened this issue Jan 29, 2021 · 2 comments

Comments

@ilyazub
Copy link

ilyazub commented Jan 29, 2021

Now only callbacks are supported for SerpApiSearch#json.

const { GoogleSearch } = require('google-search-results-nodejs')
const search = new GoogleSearch(process.env.API_KEY)

const params = {
  engine: 'google_maps',
  q: 'pizza',
  ll: '@40.7455096,-74.0083012,15.1z',
  type: 'search'
}

search.json(params, data => {
  console.log(data.local_results)
})

The workaround is to promisify functions, but out-of-the-box support would be great. Test with the workaround here.

const { GoogleSearch } = require('google-search-results-nodejs')
const search = new GoogleSearch(process.env.API_KEY)

function promisifiedGetJson(params) {
  return new Promise((resolve, reject) => {
    try {
      search.json(params, resolve)
    } catch (e) {
      reject(e)
    }
  })
}

const params = {
  engine: 'google_maps',
  q: 'pizza',
  ll: '@40.7455096,-74.0083012,15.1z',
  type: 'search'
}

async function main(params) {
  try {
    const data = await promisifiedGetJson(params)

    console.log('Local results\n')

    for (const localResult of data.local_results) {
      const { title, position, reviews } = localResult
      console.log(`Position: ${position}\nTitle: ${title}\nReviews: ${reviews}\n`)
    }
  } catch (error) {
    console.error('there was an error:', error)
  }
}

main(params)

PS. Node.js HTTPS module is used to send requests. got has a simpler API and the response stream is handled by the library.

search.get(url, (resp) => {
let data = ''
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk
})
// The whole response has been received. Print out the result.
resp.on('end', () => {
try {
if (resp.statusCode == 200) {
callback(data)
} else {
throw data
}
} catch (e) {
throw e
}
});
}).on("error", (err) => {
throw err;
});

@ilyazub
Copy link
Author

ilyazub commented Feb 16, 2021

@jvmvik @hartator What do you think?

@alanwilter
Copy link

I don't know if it helps but here I have two ways of implementing an endpoint to be used with https://shields.io/endpoint. A big thanks to @ilyazub for helping me with that.

Certainly a promise api version would make the code there much more elegant and succinct.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants