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

Tidy async/await solution for avoiding callback hell #1092

Open
ghost opened this issue Jan 29, 2020 · 2 comments
Open

Tidy async/await solution for avoiding callback hell #1092

ghost opened this issue Jan 29, 2020 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 29, 2020

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"!

@brennanagranoff
Copy link

Thanks!

@pcurz
Copy link

pcurz commented Aug 13, 2021

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

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