question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to use await and async instead callbacks?

See original GitHub issue

Currently, the code example is.:

var google = require('googleapis');
var cloudResourceManager = google.cloudresourcemanager('v1');

authorize(function(authClient) {
  var request = {
    auth: authClient,
  };

  var handlePage = function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

    var projectsPage = response['projects'];
    if (!projectsPage) {
      return;
    }
    for (var i = 0; i < projectsPage.length; i++) {
      // TODO: Change code below to process each resource in `projectsPage`:
      console.log(JSON.stringify(projectsPage[i], null, 2));
    }

    if (response.nextPageToken) {
      request.pageToken = response.nextPageToken;
      cloudResourceManager.projects.list(request, handlePage);
    }
  };

  cloudResourceManager.projects.list(request, handlePage);
});

function authorize(callback) {
  google.auth.getApplicationDefault(function(err, authClient) {
    if (err) {
      console.error('authentication failed: ', err);
      return;
    }
    if (authClient.createScopedRequired && authClient.createScopedRequired()) {
      var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
      authClient = authClient.createScoped(scopes);
    }
    callback(authClient);
  });
}

Is there a way to use this example using async/await features? My problem is handle async errors.

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
natriumptcommented, Nov 9, 2017

I had a similar issue and found out that it happens due to util.promisify() not passing the context.

An alternative solution I found was to use Bluebird’s Promisify which allows context as an option. So you would use something like:

const { promisify } = require('bluebird');
...
const authAsync = promisify(google.auth.getApplicationDefault, { context: google.auth });

Which would then bind google.auth to this and provide access to that property.

2reactions
ghostcommented, Nov 18, 2017

For someone is looking for async’d Google API functions, try googleapis-async. It’s currently not supporting for async’d google.auth, though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to "await" for a callback to return? - Stack Overflow
You can achieve this without callbacks , use promise async await instead of callbacks here how I would do this.
Read more >
JavaScript Async/Await Tutorial – Learn Callbacks, Promises ...
The keyword await makes JavaScript wait until a promise settles and returns its result. How to use the await keyword in JavaScript. Let's...
Read more >
How to convert a callback into async/await - Flavio Copes
How to convert a callback into async/await ; return new Promise((resolve, reject) => { ; //upload the file, then call the callback with...
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
The key takeaway here is that callback functions are not asynchronous— setTimeout is the asynchronous Web API responsible for handling ...
Read more >
Callback Functions | Async-Await | Promises - Enlear Academy
Await eliminates the use of callbacks in .then() and .catch(). In using async and await, async is prepended when returning a promise, await...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found