Skip to content

Instantly share code, notes, and snippets.

@lambdaydoty
Created February 22, 2019 08:09
Show Gist options
  • Save lambdaydoty/da26a5da0512d4ca0495ccc55e5c43e7 to your computer and use it in GitHub Desktop.
Save lambdaydoty/da26a5da0512d4ca0495ccc55e5c43e7 to your computer and use it in GitHub Desktop.
function printString (str) {
setTimeout(
console.log,
Math.floor(Math.random() * 100) + 1,
str
)
}
function printAll () {
printString('A')
printString('B')
printString('C')
printString('D')
printString('E')
}
// printAll()
/* Callback */
function printStringWithCb (str, cb) {
setTimeout(
x => {
console.log(x)
cb()
},
Math.floor(Math.random() * 100) + 1,
str
)
}
function printAllWithCb () {
printStringWithCb('A', () => {
printStringWithCb('B', () => {
printStringWithCb('C', () => {
printStringWithCb('D', () => {
printStringWithCb('E', () => {
})
})
})
})
})
}
// printAllWithCb()
/* Promise: using the Promise Constructor */
function printStringWithPromise (str) {
return new Promise((resolve, reject) => {
setTimeout(
() => {
console.log(str)
resolve()
},
Math.floor(Math.random() * 100) + 1,
)
})
}
function printAllWithPromise () {
return printStringWithPromise('A')
.then(() => printStringWithPromise('B'))
.then(() => printStringWithPromise('C'))
.then(() => printStringWithPromise('D'))
.then(() => printStringWithPromise('E'))
}
// printAllWithPromise()
/* Promise: using the Promisify Util */
function printStringWithPromise2 (str) {
const util = require('util')
const setTimeoutPromise = util.promisify(setTimeout)
return setTimeoutPromise(
Math.floor(Math.random() * 100) + 1,
str
).then(console.log)
}
function printAllWithPromise2 () {
return printStringWithPromise2('A')
.then(() => printStringWithPromise2('B'))
.then(() => printStringWithPromise2('C'))
.then(() => printStringWithPromise2('D'))
.then(() => printStringWithPromise2('E'))
}
// printAllWithPromise2()
/* Promise with Async/Await */
async function printAllWithAsyncAwait () {
await printStringWithPromise2('A')
await printStringWithPromise2('B')
await printStringWithPromise2('C')
await printStringWithPromise2('D')
await printStringWithPromise2('E')
}
printAllWithAsyncAwait()
/* Callback */
function addStringWithCb (prev, curr, cb) {
setTimeout(
() => {
const result = `${prev} ${curr}`
cb(result)
},
Math.floor(Math.random() * 100) + 1,
)
}
function printAllWithCb () {
addStringWithCb('', 'A', result => {
addStringWithCb(result, 'B', result => {
addStringWithCb(result, 'C', result => {
addStringWithCb(result, 'D', result => {
addStringWithCb(result, 'E', result => {
console.log(result)
})
})
})
})
})
}
printAllWithCb()
/* Promise: using the Promise Constructor */
function addStringWithPromise (prev, curr) {
return new Promise((resolve, reject) => {
setTimeout(
() => {
const result = `${prev} ${curr}`
resolve(result)
},
Math.floor(Math.random() * 100) + 1,
)
})
}
function printAllWithPromise () {
return addStringWithPromise('', 'A')
.then(result => addStringWithPromise(result, 'B'))
.then(result => addStringWithPromise(result, 'C'))
.then(result => addStringWithPromise(result, 'D'))
.then(result => addStringWithPromise(result, 'E'))
.then(console.log)
}
printAllWithPromise()
/* Promise: using the Promisify Util */
function addStringWithPromise2 (prev, curr) {
const util = require('util')
const setTimeoutPromise = util.promisify(setTimeout)
return setTimeoutPromise(
Math.floor(Math.random() * 100) + 1,
prev,
).then(prev => `${prev} ${curr}`)
}
function printAllWithPromise2 () {
return addStringWithPromise2('', 'A')
.then(result => addStringWithPromise2(result, 'B'))
.then(result => addStringWithPromise2(result, 'C'))
.then(result => addStringWithPromise2(result, 'D'))
.then(result => addStringWithPromise2(result, 'E'))
.then(console.log)
}
printAllWithPromise2()
/* Promise with Async/Await */
async function printAllWithAsyncAwait () {
let result = ''
result = await addStringWithPromise2(result, 'A')
result = await addStringWithPromise2(result, 'B')
result = await addStringWithPromise2(result, 'C')
result = await addStringWithPromise2(result, 'D')
result = await addStringWithPromise2(result, 'E')
console.log(result)
}
printAllWithAsyncAwait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment