Message from JavaScript discussions

April 2017

— 

const readFilePromise = (filename) => new Promise((resolve, reject) => fs.readFile(filename, (err, data) => {
if (err) reject(err);
else resolve(data);
}));


Can't use Promise.resolve instead of new Promise here

Message permanent page

— 

Any control flow which relies on the promise to be resolved as a matter of some async event being finished will not work... if the promise is resolved prior to that event finishing, which would be the case if you used Promise.resolve. Should be obvious to anyone who works with them routinely

— Yeah

— I mean I've used pomises in non-async situations because they are a great way out of callback hell, so theres probably very good use examples for it

Message permanent page

— Ahhh

— Why would there be callbacks in non-async situations though...

— In systems design you may have an event bus

— Like a dispatcher or other event management system

— A synchronous event bus?!

— Or, in the case of DOM nodes, state propagation

— Yes

— Mostly in systems handling state