Skip to content

Commit

Permalink
a few more promises
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgtwo committed May 24, 2024
1 parent 11322d5 commit c0aaabb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
26 changes: 26 additions & 0 deletions promises/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
async function getTodos() {
let data, err

try {
const res = await fetch('https://jsonplaceholder.typicode.com/todos')
data = await res.json()
} catch (e) {
err = e
}

return err ? err : data
}

async function getPromise() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(100), 3000)
})
}
console.log(
await getTodos()
)
setTimeout(async () => {
console.log(await getPromise())
}, 3000)


21 changes: 21 additions & 0 deletions promises/sheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
PROMISES
**/
const promise = new Promise((res, rej) => { res(123) })
const promiseFactory = async () => new Promise((res, rej) => {
setTimeout(() => res(1234), 3000)
})

console.log(await promise)
console.log(await promiseFactory())

/**
FETCH
*/

try {
const res = await fetch('https://jsonplaceholder.typicode.com/todos')
console.log(await res.json())
} catch (e) {
console.log(e)
}

0 comments on commit c0aaabb

Please sign in to comment.