Created
June 8, 2020 13:05
-
-
Save jeremiah-ang/acb9a7885ace96f9026fa6ac6f1acd33 to your computer and use it in GitHub Desktop.
Converting callback to promises to async/await
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
// Async Await | |
function showLoading() { | |
console.log('Loading...'); | |
} | |
function populateTable(data) { | |
console.log(`populate table with ${data}`); | |
} | |
function updatePagination(count) { | |
console.log(`Pagination: ${count}`); | |
} | |
function getBasicData(callback) { | |
console.log('Getting basic data'); | |
return new Promise((resolve, _) => { | |
setTimeout(() => { | |
console.log('Got basic data'); | |
const data = 'HELLO MOTO' | |
resolve(data); | |
}, 3000); | |
}) | |
} | |
function getBasicCount(callback) { | |
return new Promise((resolve, _) => { | |
console.log('Getting basic count'); | |
setTimeout(() => { | |
console.log('Got basic count'); | |
const count = 9999; | |
resolve(count); | |
}, 2000); | |
}); | |
} | |
function finish() { | |
console.log('finished') | |
} | |
function enterDataViewerPage() { | |
showLoading(); | |
getBasicCount() | |
.then(updatePagination); | |
return getBasicData() | |
.then(populateTable) | |
} | |
const enterAdvanceDataViewerPage = enterDataViewerPage; | |
// can only await promises | |
await enterDataViewerPage(); | |
await enterAdvanceDataViewerPage(); | |
finish(); |
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
// Callback | |
function showLoading() { | |
console.log('Loading...'); | |
} | |
function makePopulateTable(callback) { | |
function populateTable(data) { | |
console.log(`populate table with ${data}`); | |
callback(); | |
} | |
return populateTable; | |
} | |
function makeUpdatePagination(callback) { | |
function updatePagination(count) { | |
console.log(`Pagination: ${count}`); | |
callback(); | |
} | |
return updatePagination; | |
} | |
function getBasicData(callback) { | |
console.log('Getting basic data'); | |
setTimeout(() => { | |
console.log('Got basic data'); | |
const data = 'HELLO MOTO' | |
callback(data); | |
}, 3000); | |
} | |
function getBasicCount(callback) { | |
console.log('Getting basic count'); | |
setTimeout(() => { | |
console.log('Got basic count'); | |
const count = 9999; | |
callback(count); | |
}, 2000); | |
} | |
function enterDataViewerPage(callback) { | |
let asyncCount = 2; | |
function allAsyncDone() { | |
if (--asyncCount === 0) return callback(); | |
} | |
showLoading(); | |
getBasicData(makePopulateTable(allAsyncDone)); | |
getBasicCount(makeUpdatePagination(allAsyncDone)); | |
} | |
function finish() { | |
console.log('finished') | |
} | |
const enterAdvanceDataViewerPage = function(callback) { | |
return function() { | |
enterDataViewerPage(callback); | |
} | |
}; | |
enterDataViewerPage(enterAdvanceDataViewerPage(finish)); |
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
// Promise | |
function showLoading() { | |
console.log('Loading...'); | |
} | |
function populateTable(data) { | |
console.log(`populate table with ${data}`); | |
} | |
function updatePagination(count) { | |
console.log(`Pagination: ${count}`); | |
} | |
function getBasicData(callback) { | |
console.log('Getting basic data'); | |
return new Promise((resolve, _) => { | |
setTimeout(() => { | |
console.log('Got basic data'); | |
const data = 'HELLO MOTO' | |
resolve(data); | |
}, 3000); | |
}) | |
} | |
function getBasicCount(callback) { | |
return new Promise((resolve, _) => { | |
console.log('Getting basic count'); | |
setTimeout(() => { | |
console.log('Got basic count'); | |
const count = 9999; | |
resolve(count); | |
}, 2000); | |
}); | |
} | |
function finish() { | |
console.log('finished') | |
} | |
function enterDataViewerPage() { | |
showLoading(); | |
const basicCountPromise = getBasicCount() | |
.then(updatePagination); | |
const basicDataCount = getBasicData() | |
.then(populateTable) | |
return Promise.all([basicCountPromise, basicDataCount]); | |
} | |
const enterAdvanceDataViewerPage = enterDataViewerPage; | |
enterDataViewerPage().then(enterAdvanceDataViewerPage).then(finish); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment