JSFiddle - React, Tailwind, and code Playground
by nimishgoel056
JavaScript
async function asynGetData(data, index = 0) {
switch (data[index].state) {
case 'success':
return {title: 'Order complete', message: null}
case 'error':
switch (data[index].errorCode) {
case 'NO_STOCK':
return {title: 'Error page', message: 'No stock has been found'}
case 'INCORRECT_DETAILS':
return {title: 'Error page', message: 'Incorrect details have been entered'}
default:
return {title: 'Error page', message: null}
}
case 'processing':
await waitingFunction()
return asynGetData(data, index + 1)
}
}
function waitingFunction() {
return new Promise(function(resolve, reject) {
window.setTimeout(function() {
resolve('processing done.')
}, 2000)
})
}
function getProcessingPage(data) {
return asynGetData(data)
}
getProcessingPage([{ state: 'processing' }, { state: 'error' }]);