zxol / airbnbapi

Unofficial airbnb.com REST API wrapper for node.js
MIT License
218 stars 52 forks source link

Usage Examples in Documentation #50

Closed garrettdowd closed 4 years ago

garrettdowd commented 4 years ago

Hi there,

Could additional usage examples be included in the README for people not familiar with JS? I am coming from Python, so I am unsure exactly how to use promises. Unfortunately, I am having trouble with the basic task of printing the data from the promises to the console.

This repo seems to be one of the only viable methods to get the Airbnb data I need. Python alternatives are not great.

AdrienDS commented 4 years ago

@garrettdowd If you come from Python and you are familiar with the Twisted framework, Promises are kind of like Deferred in Twisted.

You can use them in 2 ways, using their .then() method, or awaiting for them in an async function:

// using .then
myPromise
  .then(r => console.log(r))
  .catch(e => console.error(e))

// using async/await
async function test() {
   const r = await myPromise
   console.log(r)
}

// You can also await in a try/catch block
async function test() {
   try {
      const r = await myPromise
      console.log(r)
   } catch(e) {
      console.error(e)
   }
}
garrettdowd commented 4 years ago

Sorry for leaving this open so long. Thank you for the explanation!