Is there a way to use await and async instead callbacks?
See original GitHub issueCurrently, 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:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:Which would then bind
google.auth
tothis
and provide access to that property.For someone is looking for
async
’d Google API functions, trygoogleapis-async
. It’s currently not supporting forasync
’dgoogle.auth
, though.