Skip to content

Instantly share code, notes, and snippets.

@mul14
Last active July 6, 2018 10:55
Show Gist options
  • Save mul14/cfb42d11ed4e41a5d583cc8f3e445566 to your computer and use it in GitHub Desktop.
Save mul14/cfb42d11ed4e41a5d583cc8f3e445566 to your computer and use it in GitHub Desktop.
Asynchronous in JavaScript
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
const saveToCloud = () => {
return new Promise((resolve, reject) => {
const delay = randomInt(500, 3000)
setTimeout(() => {
resolve('Save to cloud success in ' + delay + 'ms')
// reject()
}, delay)
})
}
const sendNotification = () => {
return new Promise((resolve, reject) => {
const delay = randomInt(500, 3000)
setTimeout(() => {
resolve('Send notification success in ' + delay + 'ms')
// reject()
}, delay)
})
}
const fetchNews = () => {
return new Promise((resolve, reject) => {
const delay = randomInt(500, 3000)
setTimeout(() => {
resolve('Fetch new data success in ' + delay + 'ms')
// reject()
}, delay)
})
}
(async () => {
console.log(await saveToCloud())
console.log(await sendNotification())
console.log(await fetchNews())
})()
const request = require('request')
request('url1', function (err, res, body) {
request('url2', function (err2, res2, body2) {
request('url3', function (err3, res3, body3) {
request('url4', function (err4, res4, body4) {
request('url5', function (err5, res5, body5) {
request('url6', function (err6, res6, body6) {
request('url7', function (err7, res7, body7) {
request('url8', function (err8, res8, body8) {
request('url9', function (err9, res9, body9) {
request('url10', function (err10, res10, body10) {
request('url11', function (err11, res11, body11) {
request('url12', function (err12, res12, body12) {
// Actual code
})
})
})
})
})
})
})
})
})
})
})
})
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
function saveToCloud(callback) {
const delay = randomInt(500, 3000)
setTimeout(() => {
if (typeof callback === 'function') {
callback()
}
console.log('Data has been saved to cloud service in ' + delay + 'ms')
}, delay)
}
function sendNotification(callback) {
const delay = randomInt(500, 3000)
setTimeout(() => {
if (typeof callback === 'function') {
callback()
}
console.log('Notification has been sent in ' + delay + 'ms')
}, delay)
}
function fetchNews(callback) {
const delay = randomInt(500, 3000)
setTimeout(() => {
if (typeof callback === 'function') {
callback()
}
console.log('Latest news has been fetched in ' + delay + 'ms')
}, delay)
}
saveToCloud()
sendNotification()
fetchNews()
// saveToCloud(function () {
// sendNotification(function () {
// fetchNews(() => {
//
// })
// })
// })
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
const saveToCloud = () => {
return new Promise((resolve, reject) => {
const delay = randomInt(500, 3000)
setTimeout(() => {
resolve('Save to cloud success in ' + delay + 'ms')
// reject()
}, delay)
})
}
const sendNotification = () => {
return new Promise((resolve, reject) => {
const delay = randomInt(500, 3000)
setTimeout(() => {
resolve('Send notification success in ' + delay + 'ms')
// reject()
}, delay)
})
}
const fetchNews = () => {
return new Promise((resolve, reject) => {
const delay = randomInt(500, 3000)
setTimeout(() => {
resolve('Fetch new data success in ' + delay + 'ms')
// reject()
}, delay)
})
}
// ------------------------------------------
// Get promise result
// ------------------------------------------
// const result = saveToCloud()
// result
// .then(message => {
// console.log(message)
// })
// .catch(err => {
// throw new Error('Fail')
// })
// ------------------------------------------
// Dependents
// ------------------------------------------
// saveToCloud()
// .then(messageFromSaveToCloud => {
// console.log(messageFromSaveToCloud)
// return sendNotification()
// })
// .then(messageFromSendNotification => {
// console.log(messageFromSendNotification)
// return fetchNews()
// })
// .then(messageFromFetchNews => {
// console.log(messageFromFetchNews)
// })
// .catch(err => {
// throw new Error('Fail')
// })
// ------------------------------------------
// Promise.all - All promises must resolved
// ------------------------------------------
// const result = Promise.all([
// saveToCloud(),
// sendNotification(),
// fetchNews(),
// ])
// result.then(([a, b, c]) => {
// console.log(a)
// console.log(b)
// console.log(c)
// }).catch(err => {
// throw new Error('Fail')
// })
// ------------------------------------------
// Promise.race - Who's the winner?
// ------------------------------------------
// const result = Promise.race([
// saveToCloud(),
// sendNotification(),
// fetchNews(),
// ])
// result.then(winner => {
// console.log(winner)
// }).catch(err => {
// throw new Error('Fail')
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment