Closed jadjoubran closed 4 years ago
@jadjoubran I can go ahead an implement an example for these and raise a PR for the same.
Thanks @Darth-koder007 Yes feel free! Let's do it one by one though and it would be great if you can share the code snippet in a comment here first to maintain consistency
@jadjoubran Sharing the snippet for Async/Await (Use try/catch for error handling) Have a look and provide feedback
/**
* @description Returns a promise in either fulfilled
* or rejected state after sometime
*
* @param {boolean} [shouldResolve=true] shouldResolve
* @param {number} [afterTime=2000] afterTime
*
* @return {Promise}
*/
function promiseReturn(shouldResolve = true, afterTime = 2000) {
return new Promise((resolve, reject) => {
window.setTimeout(() => {
if (shouldResolve) resolve("Promise resolved");
reject("Promise rejected");
}, afterTime);
});
}
/**
* @description Driver function for resolving or rejecting
* a promise after provided time
*
* @param {boolean} shouldResolve
* @param {boolean} afterTime
*
* @return {void}
*/
async function resolveOrRejectPromise(shouldResolve, afterTime) {
let promiseMessage = "";
try {
promiseMessage = await promiseReturn(shouldResolve, afterTime);
} catch (e) {
promiseMessage = e;
}
alert(promiseMessage);
}
// Rejects the promise after 2000ms
resolveOrRejectPromise(false, 2000);
// Fulfils the promise after 3000ms
resolveOrRejectPromise(true, 3000);
@jadjoubran sharing snippet for use case cancel timeout
let timeOutID = window.setTimeout(() => {
console.log('I will not be printed');
}, 2000);
window.clearTimeout(timeOutID);
@jadjoubran sharing snippet for use case cancel interval
let intervalID = window.setInterval(() => {
console.log('I will not be printed');
}, 2000);
window.clearInterval(intervalID);
@jadjoubran Please have a look
Thanks @Darth-koder007, sorry got busy in the last month
The clearInterval and clearTimeout look good 👍 can you please submit them as a PR? It would be great if you can use const
rather than let
to align with the other use case
the async/await
example is way too big for codetogo as most of the usecases here are under 5 lines long.
Maybe it makes sense to use async/await on a function that returns a promise, such as fetch?
@jadjoubran How's this for async/await example ?
(async () => {
let anon = await fetch('/');
console.log(await anon.text())
})();
Thanks @Darth-koder007 Can you please use this example and make it into an async/await call? https://codetogo.io/how-to-fetch-json-in-javascript/ It's also probably a good idea to wrap it in a try/catch so that users remember to handle errors
@jadjoubran I have incorporated your suggestion Please have a look
(async () => {
try {
const fetchResponse = await fetch("https://codetogo.io/api/users.json");
console.log(await fetchResponse.json());
} catch(err){
console.error(err);
}
})();
Thanks a lot @Darth-koder007! Can you please go with this one instead?
(async () => {
try {
const response = await fetch("https://codetogo.io/api/users.json");
const data = await response.json();
console.log(data);
} catch(err){
console.error(err);
}
})();
I just renamed a variable and added another one to make it clearer
You can use the fetch-json.md
as a template as it already contains the output