Created
July 7, 2020 10:03
-
-
Save it-one-mm/483f1b99c1ef34c82b2155bc3fc1ec4c to your computer and use it in GitHub Desktop.
Async Await Course in React Native from IT ONE MM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getCustomer(1, (customer) => { | |
console.log('Customer: ', customer); | |
if (customer.isGold) { | |
getTopMovies((movies) => { | |
console.log('Top movies: ', movies); | |
sendEmail(customer.email, movies, () => { | |
console.log('Email sent...') | |
}); | |
}); | |
} | |
}); | |
function getCustomer(id, callback) { | |
setTimeout(() => { | |
callback({ | |
id: 1, | |
name: 'Mosh Hamedani', | |
isGold: true, | |
email: 'email' | |
}); | |
}, 4000); | |
} | |
function getTopMovies(callback) { | |
setTimeout(() => { | |
callback(['movie1', 'movie2']); | |
}, 4000); | |
} | |
function sendEmail(email, movies, callback) { | |
setTimeout(() => { | |
callback(); | |
}, 4000); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// getCustomer(1, (customer) => { | |
// console.log('Customer: ', customer); | |
// if (customer.isGold) { | |
// getTopMovies((movies) => { | |
// console.log('Top movies: ', movies); | |
// sendEmail(customer.email, movies, () => { | |
// console.log('Email sent...') | |
// }); | |
// }); | |
// } | |
// }); | |
async function notifyCustomer() { | |
const customer = await getCustomer(1); | |
console.log('Customer: ', customer); | |
if (customer.isGold) { | |
const movies = await getTopMovies(); | |
console.log('Top movies: ', movies); | |
await sendEmail(customer.email, movies); | |
console.log('Email sent...'); | |
} | |
} | |
notifyCustomer(); | |
function getCustomer(id) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve({ | |
id: 1, | |
name: 'Mosh Hamedani', | |
isGold: true, | |
email: 'email' | |
}); | |
}, 4000); | |
}); | |
} | |
function getTopMovies() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(['movie1', 'movie2']); | |
}, 4000); | |
}); | |
} | |
function sendEmail(email, movies) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(); | |
}, 4000); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment