Promises - Part 8 of Functional Programming in JavaScript
Why Stack Overflow recommends it
Stack Overflow users link it when answering questions where asynchronous JavaScript behaves unexpectedly, such as pending promises, Promise.all, or code running out of order.
Recommended by 5 people across 5 different months, 2017 to 2020.
What it is
A 17-minute episode of the Fun Fun Function series in which MPJ explains JavaScript promises as a more composable alternative to callbacks. It covers chaining and combining promises with live code examples.
What people said
Recommends Bharathvaj Ganesan · 2017-08 · Stack Overflow · on “How do I set user_id automatically in MySQL using Nodejs?”
Use MYSQL AUTO_INCREMENT feature to create a new user_id for each new user (i.e each new row) while inserting as follows CREATE TABLE USERS( user_id INT NOT NULL AUTO_INCREMENT); cheers For Callbacks and Promises checkout this video [this video] and for latest await /async feature this video [LINK]
Recommends James Poag · 2018-06 · Stack Overflow · on “How do I properly grab the users profile from Realtime Database to get their username before the cloud function returns?”
Well, you don't really get to do anything before the cloud function returns. Here's a fun fun function video on Promises [this video] . Basically, you're passing a function into the promise to be executed when the datasnapshot is retrieved. Promise.All waits on all promises to finish before running the code you've sent it. It's all asynchronous. With that in mind, it looks like you are trying to orchestrate a lot of code to complete. Perhaps there's a better way to structure your calls/DB to avoid most of it? The Free tier has reasonable execution limits.
Recommends ykit9 · 2018-08 · Stack Overflow · on “Promise pattern for nested readFiles in node”
You've mistaken when you tried to read includes and do not wait for the results. The promise gets resolved with zero data before the first include file is read. This should work: serviceReturnJsonPromise = function () { return readFile(masterPath, 'utf8', err) .then(function (masterData) { var master = JSON.parse(masterData); // map over includes array and return "read" promises var includes = master.includes.map(function (item) { var includePath = "./" + item.replace("#", ""); return readFile(includePath, 'utf8', err).then(function (includeData) { return JSON.parse(includeData) }); }); // wait for all file reads return Promise.all(includes).then(jsons=>{ master.includes = jsons; return mast
Recommends Producdevity · 2018-10 · Stack Overflow · on “Javascript Async/Await program order”
@lavender that is exactly what is happening, because you didn't wait (or await) until the promise resolved and just logged out the pending Promise. I think this video of MPJ does a great job explaining how the confusing part of Promises work. [this video]
Recommends DauleDK · 2019-07 · Stack Overflow · on “i can't understand the order in which my code is executing”
To understand why you see this behaviour, you need to understand asynchronous code. Nodejs will read your instructions, and then it's done. When the find method then finishes, it calls the callback defined. Imagine the find operation was very complicated, then your create function callback would be executed first. (but the find operation would be executing meanwhile). To understand this, I can recommend the following videos. Make some coffee and tune in :) What the heck is the event loop anyway? | Philip Roberts | JSConf EU [LINK] Today most Nodejs programmers use Promises to handle async code, you can learn about this here: Promises - Part 8 of Functional Programming in JavaScript [this video] A
Cites Nebula · 2017-06 · Stack Overflow · on “Promise All in Node.js with a forEach loop”
In recent versions of Node (8.0.0 and later), there's a new util.promisify [LINK] function you can use to get a promise. Here's how we might use it: // Of course we'll need to require important modules before doing anything // else. const util = require('util') const fs = require('fs') // We use the "promisify" function to make calling promisifiedReaddir // return a promise. const promisifiedReaddir = util.promisify(fs.readdir) // (You don't need to name the variable util.promisify promisifiedXYZ - // you could just do `const readdir = util.promisify(fs.readdir)` - but // I call it promisifiedReaddir here for clarity. function createFiles(countryCode) { // Since we're using our promisified r
Cites Alejandro A. · 2020-12 · Stack Overflow · on “How can I get the properties of a nested object with strange characters such as []?”
The reason why you cannot access your data at that point of the runtime is because response response.json() returns a Promise, not the actual data, so one option is to await as pointed out by @Keith above, or use then() . If you want to dig deeper into promises, async/await, and even as historical learning, the callback(err, res) approach, I suggest you to check out: async/await [LINK] promises [this video]