Last active
September 21, 2016 10:24
-
-
Save Ginden/221c74498633ba88e57a4f3a00ae01a9 to your computer and use it in GitHub Desktop.
Migrating from callbacks to Bluebird
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const async = require('async'); | |
async.filter(['file1','file2','file3'], function(filePath, callback) { | |
fs.access(filePath, function(err) { | |
callback(null, !err) | |
}); | |
}, function(err, results){ | |
// results now equals an array of the existing files | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Promise = require('bluebird'); | |
const fs = Promise.promisifyAll(require('fs')); | |
Promise.filter(['file1','file2','file3'] , path=>fs.accessAsync(path)) | |
.then(results=>{ | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const basicArithmeticsAsService = require('fictional-service'); | |
basicArithmeticsAsService.add(2,2, (err, response) => { | |
if (err) { | |
return someCallback(new TypeError('Network issues': + err.message)); | |
} | |
if (response.status === 200) { | |
return basicArithmeticsAsService.multiply(response.body, 4, (err, response) => { | |
if (err) { | |
return someCallback(new TypeError('Network issues': + err.message)); | |
} else if (response.status === 200) { | |
return someCallback(null, response.body) | |
} else { | |
someCallback(null, NaN); | |
} | |
}) | |
} else { | |
someCallback(null, NaN); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const basicArithmeticsAsService = require('fictional-service'); | |
basicArithmeticsAsService.addAsync(2,2) | |
.then((response) => { | |
if (response.status === 200) { | |
return basicArithmeticsAsService.multiplyAsync(response.body, 4) | |
.then(response=>{ | |
if (response.status === 200) { | |
return response.body; | |
} else { | |
return NaN; | |
} | |
}) | |
} else { | |
return NaN; | |
} | |
}) | |
.catch(err=>{ | |
throw new TypeError('Network issues': + err.message); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
fs.readFile('someFile.txt', 'utf8', function(err, fileContent) { | |
if(err) { | |
throw err; // Actually ends process | |
} | |
fs.readFile(fileContent, function(err, image) { | |
if(err) { | |
throw err; | |
} | |
doStuff(image); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Promise = require('bluebird'); | |
const fs = Promise.promisifyAll(require('fs')); | |
fs.readFileAsync('someFile.txt', 'utf8') | |
.then(fileContent=>fs.readFileAsync(fileContent)) | |
.then(doStuff); | |
.catch(err=>{ | |
// Handle all errors in one place | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const async = require('async'); | |
retry(5, done=>getResource(url, done), (err, result)=>{ | |
if (err) throw err; | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const async = require('async'); | |
function retry(n=5, task) { | |
return Promise.resolve().then(task).catch(err=>{ | |
if (n > 1) return retry(n-1, task); | |
throw err; | |
}); | |
} | |
retry(()=>getResourceAsync(url)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function retry(fn, maxRetries, complete) { | |
fn((err, res)=>{ | |
if (err) { | |
if (!maxRetries) { | |
complete(err); | |
} else { | |
retry(fn, maxRetries-1, complete); | |
} | |
} else { | |
complete(err, res); | |
} | |
}) | |
} | |
retry(complete=>{ | |
return async.parallel([ | |
aquireResource, | |
aquireOtherResource | |
], complete); | |
}, 5, console.log) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function settleAll(promises) { | |
return Promise.all(promises.map(promise => Promise.resolve(promise).reflect())) | |
.then(function(results) { | |
const err = results.find(e=>e.isRejected()); | |
if (err) { | |
throw err.reason; | |
} else { | |
return results.map(e=>e.value); | |
} | |
}); | |
} | |
function retry(task, maxRetries) { | |
return task().catch(e=>{ | |
if (!maxRetries) return retry(task, maxRetries-1); | |
throw e; | |
}); | |
} | |
retry(()=>settleAll([task1(), task2()]), 5).then(console.log, console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment