Last Updated on: (senast uppdaterad på:)
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 });
- https://stackabuse.com/how-to-use-module-exports-in-node-js/
- https://blog.risingstack.com/mastering-async-await-in-nodejs/
- https://stackoverflow.com/questions/32850045/node-js-synchronous-queries-with-mysql
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/
How To Develop and Build Angular App With NodeJS
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(); } }); });