Skip to content

Instantly share code, notes, and snippets.

View niinpatel's full-sized avatar
💭
goofing off

Nitin Patel niinpatel

💭
goofing off
View GitHub Profile
@niinpatel
niinpatel / asyncawait.js
Last active June 9, 2018 20:12
Using Async Await to create asynchronous code.
/*
An aync function that takes two numbers and performs some operations
*/
performOperations(2,3).then(output => console.log(output)).catch(err => console.log(err));
async function performOperations(a,b) {
let sum = await addTwoNos(a, b);
let doubleOfSum = await double(sum);
@niinpatel
niinpatel / Promises.js
Last active June 9, 2018 20:12
Using promises for asynchronous execution.
/*
Suppose I want to add two numbers, then double it, then square that number, then add double it again. Using Promises.
*/
addTwoNos(2, 3)
.then(sum => double(sum))
.then(doubleOfSum => square(doubleOfSum))
.then(square => double(square))
.then(output => console.log(output))
.catch(err => console.log(err));
@niinpatel
niinpatel / simple callback hell.js
Last active June 9, 2018 18:39
An example of a callback hell
/*
Suppose I want to add two numbers, then double it, then square that number, then add double it again.
*/
addTwoNos(2, 3, (sum) => {
double(sum, (doubleOfSum) => {
square(doubleOfSum, (square) => {
double(square, (doubleOfSquare) => {
console.log(doubleOfSquare)
})
})