dropbox-v2-api icon indicating copy to clipboard operation
dropbox-v2-api copied to clipboard

Tidy async/await solution for avoiding callback hell

Open ghost opened this issue 5 years ago • 2 comments

For anyone using modern browsers/node, this should help to make the examples usable.

The secret lies in util.promisify!

const util = require('util')
const dbAccessToken = 'YOURTOKEN'
const dropbox = require('dropbox-v2-api').authenticate({ token: dbAccessToken })
const dbSync = util.promisify(dropbox)

async function getAcc () {
  const params = { resource: 'users/get_current_account' }
  try {
    return (await dbSync(params))
  } catch (err) {
    console.error(err)
  }
}

async function test () {
  console.log(await getAcc())
}

test()

Hope this helps someone else avoid a good couple of hours struggling with why the "returns" were returning" undefined"!

ghost avatar Jan 29 '20 16:01 ghost

Thanks!

brennanagranoff avatar Feb 06 '20 02:02 brennanagranoff

However this does not work for files/list_folder, returns search_file_metadata thus crashing when writing the stream

pcurz avatar Aug 13 '21 18:08 pcurz