How do I access the data in a promise? I use .then()
:promise
function getFirstUser() { return getUsers().then(function(users) { return users[0].name; }); }
Cool… so how does async/await tie in?
Well, consider the above code. getUsers()
returns a promise. Any promise we have, using ES2016, we can await. That’s literally all await means: it functions in exactly the same way as calling .then()
on a promise (but without requiring any callback function). So the above code becomes:async
async function getFirstUser() { let users = await getUsers(); return users[0].name; }