This is why I mentioned earlier that we should be using more precise CS terminology such as concurrency and parallelism, i.e.
This is incorrect, Future
does not work with callbacks (at least if we are talking about composition/working with Future
’s). Callbacks are anonymous functions (aka thunks) that are passed directly into function calls, taken from http://callbackhell.com/ with Javascript which coined the term “callback hell”,
fs.readdir(source, function (err, files) {
if (err) {
console.log('Error finding files: ' + err)
} else {
files.forEach(function (filename, fileIndex) {
console.log(filename)
gm(source + filename).size(function (err, values) {
if (err) {
console.log('Error identifying file size: ' + err)
} else {
console.log(filename + ' : ' + values)
aspect = (values.width / values.height)
widths.forEach(function (width, widthIndex) {
height = Math.round(width / aspect)
console.log('resizing ' + filename + 'to ' + height + 'x' + height)
this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
if (err) console.log('Error writing file: ' + err)
})
}.bind(this))
}
})
})
}
})
As you can see with the fs.readdir
/files.forEach
/widths.forEach
functions, they accept another function as the last argument which is your callback. Future
’s do not take anonymous functions in their arguments, it’s completely different.
What Future
/Promise
allows is you to convert from callback style to monadic map
/flatMap
(or in JS land .then()
style) which along with syntax sugar/yield/generators gets rid of the nesting. Even Scala.js documentation has a section on how to use Future’s to get rid of callbacks (see the Future
section in From ES6 to Scala: Advanced - Scala.js)