Leandro Nunes
a.k.a. "Little Big Nunes". I'm a web development enthusiast who believes Little things make a Big difference!
function loginUser(objUser) {
console.log('logging user...')
//... some asynchronous code (i.e. ajax POST request)
console.log('User is logged !')
};
function showPage() {
//...
console.log('DONE')
})
const objUser = {user: 'leandrof', password: '123456'}
loginUser(objUser)
showPage()
// logging user...
// DONE
// User is logged !
function loginUser(objUser, cbFunc) {
console.log('logging user...')
//... some asynchronous code (i.e. ajax POST request)
console.log('User is logged !')
cbFunc()
};
function showPage() {
//...
console.log('DONE')
})
const objUser = {user: 'leandrof', password: '123456'}
loginUser(objUser, showPage)
// logging user...
// User is logged !
// DONE
loginUser(objUser, (err, response) => {
if (err) { throw 'Some error' }
getUserInfo(response, (err, response) => {
if (err) { throw 'Some error' }
getUserPermissions(response, (err, response) => {
if (err) { throw 'Some error' }
getUserCourses(response, (err, response) => {
if (err) { throw 'Some error' }
// DONE
showPage(response)
})
})
})
})
function myPromiseFunction() {
return new Promise((resolve, reject) => {
// Some asynchronous code...
// resolve on success
resolve(true)
// OR reject on failure
reject(false)
});
}
myPromiseFunction()
.then(console.log) // true
.catch(console.error) // false
function loginUser(objUser) => { //... }
function getUserInfo(objLoggedUser) => { //... }
function getUserPermissions(objLoggedUserInfo) => { //... }
function getUserCourses(objLoggedUserInfo) => { //... })
function showPage(objLoggedUserInfo) { //... })
loginUser(objUser)
.then(getUserInfo)
.then(getUserPermissions)
.then(getUserCourses)
.then(showPage); // DONE
.catch(err => {
throw 'Some error'
});
async function myAsynchronousFunction() {
return new Promise((resolve, reject) => {
// ...
});
}
await myPromiseFunction()
async function loginUser(objUser) => { //... }
function showPage() { //... })
const objUser = {user: 'leandrof', password: '123456'}
await loginUser(objUser)
showPage()
// SyntaxError: await is a reserved word
async function loginUser(objUser) => { //... }
function showPage() { //... })
(async () => {
const objUser = {user: 'leandrof', password: '123456'}
await loginUser(objUser)
showPage()
})()
// logging user...
// User is logged !
// DONE
async function loginUser(objUser) => { //... }
async function showPage() { //... })
(async () => {
const objUser = {user: 'leandrof', password: '123456'}
try {
await loginUser(objUser)
showPage()
} catch(err) {
console.error(err)
}
})()
async function loginUser(objUser) => { //... }
async function getUserInfo(objLoggedUser) => { //... }
async function getUserPermissions(objLoggedUserInfo) => { //... }
async function getUserCourses(objLoggedUserInfo) => { //... })
function showPage(objLoggedUserInfo) { //... })
(async () => {
const objUser = {user: 'leandrof', password: '123456'}
try {
const objLoggedUser = await loginUser(objUser)
const objUserInfo = await getUserInfo(objLoggedUser)
const objUserPermissions = await getUserPermissions(objUserInfo)
const objUserCourses = await getUserCourses(objUserInfo)
showPage(objUserCourses)
} catch(err) {
console.error(err)
}
})()
By Leandro Nunes
Asynchronous code using async/await (ES8)
a.k.a. "Little Big Nunes". I'm a web development enthusiast who believes Little things make a Big difference!