I just reworked my previously posted code access a google photos album list to use async/await and think it gives a good account of itself. It’s not a massive change but I think easier to read and less boiler plate.
As Azure Functions only support nodejs 6.5.0, I switched to typescript in order to use async/await but I want to use it any way. More versions of node will be supported (it’s work in progresss) so you will be able to use async/await without typescript’s extra transpilation step.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
function getPhotoAlbumsList( context, req ) {
getAdminAccessToken()
.then(({ object: { access_token } }) => {
const userId = req.user.sub
return getUserProfile(access_token, userId)
})
.then(({ object }) => {
const google_access_token = object.identities[0].access_token
return getPhotoAlbums(google_access_token)
})
.then(({ object: { feed: { entry } } }) => {
const titles = entry.map((ent: any) => ent.title.$t)
return {
status: 200,
body: JSON.stringify(titles),
headers: { 'Content-Type': 'application/json' }
}
})
.catch(err => {
return {
status: 400,
body: err.message
}
})
.then(res => {
context.done(null, res)
})
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
async function getPhotoAlbumsList( context: HttpContext, req: Auth0FunctionRequest): Promise<void> {
try {
const { access_token: admin_access_token } = await getAdminAccessToken()
const userId = req.user.sub
const { identities } = await getUserProfile(admin_access_token, userId)
const google_access_token = identities[0].access_token
const titles = await getGooglePhotoAlbumList(google_access_token)
const titlesText = JSON.stringify(titles)
context.done(null, {
status: 200,
body: titlesText,
headers: { 'Content-Type': 'application/json' }
})
} catch (err) {
context.done(null, {
status: 400,
body: err.message
})
}
}
|
Categories: Uncategorized
1 reply ›