Created
August 21, 2019 09:00
-
-
Save neuralline/c859d0910acf25bb9ea62437f6b8cfeb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** @format */ | |
//@ts-check | |
/** | |
Vodafone coding challenge | |
You have been tasked with creating a helper function that will be used to determine the output | |
of an array of data. | |
Each element of the array has the following structure: | |
{ | |
state: <String> - a state to go to | |
errorCode: <String> - optional error code | |
} | |
The states have different functionalities: | |
'processing' = delay by 2 seconds, then fetch the next state | |
'error' = handle the error code provided (see below) | |
'success' = return from the helper with the object: { title: 'Order complete' message: null } | |
Handling error codes: | |
'NO_STOCK' = return from the helper with an object: { title: 'Error page', message: 'No stock has been found' } | |
'INCORRECT_DETAILS' = return from the helper with an object: { title: 'Error page', message: 'Incorrect details have been entered' } | |
null = return from the helper with an object: { title: 'Error page', message: null } | |
undefined = return from the helper with an object: { title: 'Error page', message: null } | |
Example usage: | |
------- | |
getProcessingPage([{ state: 'processing' }, { state: 'error' }]) | |
=> should return after 2 seconds with the object: { title: 'Error page', message: null } | |
Notes: | |
- Provide the code and a description of how to run it | |
**/ | |
/** | |
* Gets the processing page | |
* @param {array} data | |
*/ | |
const getProcessingPage = async (data = []) => { | |
const error_code = { | |
NO_STOCK: { | |
title: 'Error page', | |
message: 'No stock has been found' | |
}, | |
INCORRECT_DETAILS: { | |
title: 'Error page', | |
message: 'Incorrect details have been entered' | |
}, | |
null: {title: 'Error page', message: null}, | |
undefined: {title: 'Error page', message: null} | |
} | |
const state = { | |
processing: code => { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve(state[code.state](code.errorCode || null)) | |
}, 2000) | |
}) | |
}, | |
error: code => error_code[code], | |
success: code => { | |
return {title: 'Order complete', message: null} | |
} | |
} | |
return await state[data[0].state](data[1]) | |
} | |
/** | |
* | |
*/ | |
const result = getProcessingPage([{state: 'success'}, {state: 'error'}]) | |
result.then(data => console.log('log result: ', data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment