Skip to content

Instantly share code, notes, and snippets.

@it-one-mm
Created July 7, 2020 10:03
Show Gist options
  • Save it-one-mm/483f1b99c1ef34c82b2155bc3fc1ec4c to your computer and use it in GitHub Desktop.
Save it-one-mm/483f1b99c1ef34c82b2155bc3fc1ec4c to your computer and use it in GitHub Desktop.
Async Await Course in React Native from IT ONE MM
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);
}
// 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