0

Trying to figure out how to turn this callback scenario into async / await

it's on this page: https://github.com/XeroAPI/xero-node

[excerpt]

/* Called per page */
const onContacts = (err, response, cb) => {
    let contacts = response.data;
    if (response.finished) // finished paging
        ....
    cb(); // Async support
};

xeroClient.core.contacts.getContacts({ pager: {start:1 /* page number */, callback: onContacts}})
    .catch(err => {
        console.log(`Oh no, an error: ${err}`);
    });

I haven't quite got my head around callbacks 100% , but I prefer the async / await structure and syntax. Is it possible with this?

Thanks for your time.

EDIT:

I need to be able to add options to page through contacts , so if you could post a snippet with the actual paging in it .. I tried the following but it didn't work ( no error or anything ) .

const opts = {
    pager: {start:1} 
}

const getContacts = async(opts) => {
    try {
      const contacts = await xeroClient.core.contacts.getContacts(opts);
    }
    catch(e) {
        console.log(e)
      //when error
    }
   };
Martin Thompson
  • 3,415
  • 10
  • 38
  • 62

1 Answers1

0

I figured it out.

async function getContacts(){
    try {
        let contacts = await xeroClient.core.contacts.getContacts({
            params: {
                Page: 1
            }           
        })
        return contacts


    } catch (error) {
        console.log(error)
    }
}
Martin Thompson
  • 3,415
  • 10
  • 38
  • 62