An archive of some useful Node.js techniques along side Angular

The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise.

const promise1 = new Promise((resolve, reject) => {
  resolve('Success!');
});

promise1.then((value) => {
  console.log(value);
  // expected output: "Success!"
});

The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input’s promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.

const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then((values) => {
  console.log(values);
});
// expected output: Array [3, 42, "foo"]

If you’re using Promises, check out this

you can just fix this by doing the following:

var promises = [];

for(var numb in req.body)
{
    promises.push(checkValue(numb));
}

Promise.all(promises)    
 .then(function(data){ /* do stuff when success */ })
 .catch(function(err){ /* error handling */ });

function checkValue(numb){
 return new Promise(function(resolve, reject){
  // place here your logic
  // return resolve([result object]) in case of success
  // return reject([error object]) in case of error
});

As jfriend00 said above, if you’re going to develop in node.js, then you MUST become comfortable with writing async code.

“chained promises” is probably your best bet:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

http://html5hive.org/node-js-quickies-working-with-mysql/

https://medium.com/bb-tutorials-and-thoughts/how-to-develop-and-build-angular-app-with-nodejs-e24c40444421

How To Develop and Build Angular App With NodeJS

https://medium.com/bb-tutorials-and-thoughts/packaging-your-angular-app-with-nodejs-backend-for-production-97eb1b0755aa

Packaging Your Angular App With NodeJS Backend For Production

Promises chaining

  • An example for acces to a MySQL call which we require the results for before returning results to user:
exports.getAllSizeIdsForItem = function (getItemId, callback) {
	sqlQuery = "SELECT size_id FROM logic20_rule WHERE item_id='" + getItemId + "'";
	arrSizeIds=[];
	db.runQuery(sqlQuery, function (getResult1) {
		for (m1=0; m1<getResult1.length; m1++) {
			arrSizeIds.push(getResult1[m1].size_id);
			if (m1==getResult1.length-1) {
				callback (arrSizeIds);
			}
		}
	})
}
exports.getSizeDetails = async function(arrSizeIds, res) {
	getSizeDetails_promises = [];
	for (mc1 = 0; mc1<arrSizeIds.length; mc1++) {	
	  await new Promise(next=> {	
		console.log("- " + mc1);
		sqlQuery = "SELECT * FROM logic20_size WHERE id='" + arrSizeIds[mc1] + "'";
		db.runQuery(sqlQuery, function (getResult1) {
			console.log("- logic10 -- category details");
			getSizeDetails_promises.push (getResult1[0]);
			next();
		})
	  })
	}
	Promise.all(getSizeDetails_promises)
		.then(function(data) {	console.log("done, now sending res with details: " + data);
								res.send(data); })
		.catch(function(err) {  console.log("error in Promise.all..." + err);  });
}
	logic20Service.getAllSizeIdsForItem(theItemId, function(getArrSizeIds) {
	    console.log("getArrSizeIds: " + getArrSizeIds);
	    logic20Service.getSizeDetails(getArrSizeIds, res);
	})
function callback () { console.log('all done'); }

var itemsProcessed = 0;

[1, 2, 3].forEach((item, index, array) => {
  asyncFunction(item, () => {
    itemsProcessed++;
    if(itemsProcessed === array.length) {
      callback();
    }
  });
});