Promise Fallthrough
NodeJs based, proper/improper way to handle promises. The key is brought in from http://stackoverflow.com/questions/43545718/returning-promises-within-asynchronous-functions
by Luis Perez
JavaScript
/*
Seemingly bypassing my Promise's reject statements
This does not only happen with fs, but with other none natively promise based sub
libraries.
Is it because I am not inherrently promisifying things, should I just return a Promise.resolve
within the sub Asynchronous function, and attach a return to it, like the format for the 2nd function?
I may be understanding the control flow of the promises incorrectly, when everything is good within the fs.readFile things will go as they should, is the async code not simulating a return when it meets that reject?
It just returns the result to that awaiting Promise, and then the Async code is left hanging around, doing whatever it wants.
Is it basically just treating the whole thing like one big generator, without a yield telling it to stop executing at this point?
*/
const Promise = require('bluebird'),
fs = require('fs'),
fName = './tmpFs.txt';
const retPromExample = () => {
return new Promise((resolve, reject) => {
fs.readFile(fName, (err, fd) => {
if (err) {
// The if condition is applied and satisfied
console.log("\nI got in here, and I know I've failed %s\n", err);
reject(err)
}
// It still gets here, even though err is clearly not null, if the file does not exist
console.log("I'm outside the if(err) with ", err);
// Do something with the file, try and JSON.parse it, and fd is undefined.
resolve("What I wanted to do was done. Resolving\n")
})
})
};
const retPromExample_deux = () => {
// On the rejection getting an unhandled rejection error as well. Am I following the syntax for this correctly?
return new Promise((res, rej) => {
return fs.readFile(fName, (err, fd) => {
if (err) {
// The if condition is applied and satisfied
return Promise.reject("Part Deux; I got in here, and I know I've failed %s \n", err)
}
// Under this case an explicit return is given, it should not reach here. If the...