Skip to content

Instantly share code, notes, and snippets.

@timaschew
Created June 29, 2017 08:47
Show Gist options
  • Save timaschew/8aeab1b41760396ab26d61e5ed22d02e to your computer and use it in GitHub Desktop.
Save timaschew/8aeab1b41760396ab26d61e5ed22d02e to your computer and use it in GitHub Desktop.
const fs = require('fs')
function async1(callback) {
console.log('1')
setTimeout(function() {
console.log('2')
if (Math.random() > 0.5) {
return callback(new Error('boom'))
}
return callback(null, 'config.txt')
}, 100)
console.log('4')
return 5 // useless
}
function async2(fileName, options, callback) {
fs.readFile(fileName, options, callback)
}
async1(function(err, value) {
if (err) {
return console.error(err)
}
return async2(value, {encoding: 'utf8'}, function(err, result) {
if (err) {
return console.log(err)
}
console.log(result.toString())
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment