Skip to content

Instantly share code, notes, and snippets.

@daniel-vera-g
Created January 20, 2018 06:43
Show Gist options
  • Save daniel-vera-g/bd660e23d3152dcfe148d49dae8c6805 to your computer and use it in GitHub Desktop.
Save daniel-vera-g/bd660e23d3152dcfe148d49dae8c6805 to your computer and use it in GitHub Desktop.
Asynchronous Javascript
/*
Different Methods to make code work the sync-way:
1. Callbacks
- Problem --> Callback hell
2. Promises
- Problem --> Not DRY-Method comfortable
3. Async/Await
*/
/* Examples:
Tasks:
1. Verify the username and password of a user.
2. Get application roles for the user.
3. Log application access time for the user.
*/
// Callbacks
/* Each function gets an argument which is another function that is called with a parameter that is the response
of the previous action */
// function to verify user
// Callback to show errors OR return the user info and roles
const verifyUser = function(username, password, callback) {
// verify and get user info
dataBase.verifyUser(username, password, (error, userInfo) => {
if (error) {
callback(error);
} else {
// get the roles of the user with the username from user
dataBase.getRoles(username, (error, roles) => {
if (error) {
callback(error);
} else {
// log user in and if successfull give callback with usreInfo and roles
dataBase.logAccess(username, error => {
if (error) {
callback(error);
} else {
callback(null, userInfo, roles);
}
});
}
});
}
});
};
/*
get roles function with callbacks
*/
const getRoles = function(username, callback) {
database.connect(connection => {
connection.query("get roles sql", result => {
callback(null, result);
});
});
};
// Promises
// All the functions have to be promisfied
// main function
const verifyUser = function(username, password) {
database
.verifyUser(username, password)
.then(userInfo => dataBase.getRoles(userInfo))
.then(rolesInfo => dataBase.logAccess(rolesInfo))
.then(finalResult => {
//do whatever the 'callback' would do
})
.catch(err => {
//do whatever the error handler needs
});
};
/*
get roles function with promises
*/
const getRoles = function(username) {
return new Promise((resolve, reject) => {
database.connect(connection => {
connection.query("get roles sql", result => {
resolve(result);
});
});
});
};
/* return a Promise, with two callbacks
Promise itself performs actions from the method
Now, resolve and reject callbacks will be mapped to Promise.then and Promise.catch methods respectively.
*/
// Async/Await
/*
Problem with Promises:
still had to rely on callbacks that are passed on to
.then and .catch methods of a Promise
*/
// write Promise-based code as if it were synchronous, but without blocking the main thread:
const verifyUser = async function(username, password) {
try {
const userInfo = await dataBase.verifyUser(username, password);
const rolesInfo = await dataBase.getRoles(userInfo);
const logStatus = await dataBase.logAccess(userInfo);
return userInfo;
} catch (e) {
//handle errors as needed
}
};
/*
Declaring a function as async will ensure that it
always returns a Promise so you don’t have to worry about that anymore
*/
// Source of code and task: https://www.toptal.com/javascript/asynchronous-javascript-async-await-tutorial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment