


Node.js Express and Promises Not Behaving as Expected
First Promise Not Waiting for findUser() Completion
In the provided code, the first promise is not waiting for findUser() to return before proceeding because:
- .findUser() asynchronously queries the database using a callback function.
- The callback is not used to resolve the promise. Instead, the promise is resolved with rows, which is undefined at the time of the promise creation.
Solution:
Wrap the database query in a function that returns a promise and resolve the promise with the query result:
me.findUser = function(params, res) { var username = params.username; return new Promise(function (resolve, reject) { pool.getConnection(function (err, connection) { console.log("Connection "); if (err) { console.log("ERROR 1 "); res.send({"code": 100, "status": "Error in connection database"}); reject(err); // Reject the promise with the error } else { connection.query('select Id, Name, Password from Users ' + 'where Users.Name = ?', [username], function (err, rows) { connection.release(); if (!err) { resolve(rows); // Resolve the promise with the query result } else { reject(err); // Reject the promise with the error } }); } }); }); }
Why error handler second is Outputted Instead of Failed
The error handler for the second promise is called because the first promise is rejected. However, the console.log("Failed"); line in the error handler is not executed because an error is thrown inside the .then() block.
To correctly handle the rejection of the first promise, use .catch() instead of .then():
promise.then(function(data) { return new Promise(...); }, function (reason) { console.log("Failed"); });
The above is the detailed content of Why is the Second Promise Error Handler Executed Before the First Promise's 'Failed' Output?. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version
Chinese version, very easy to use
