Programmatically add arguments to a Promise - javascript

I need to create a function that receives a Promise as a first parameter and an array of its parameters (if it has at least one) or null if none.
My function looks this way:
var executeMappingStep = (promiseStatus, myPromise, myPromiseParams) => {
return new Promise((resolve, reject) => {
//if the execution was success, there's no need to invoke myPromise again
if(promiseStatus.success == true)
return resolve(promiseStatus.previousResponseSaved);
if (myPromiseParams!= null) {
//how I resolve this logic is my doubt
myPromise.addArrayOfParameters(myPromiseParams);
}
myPromise().then(result => resolve(result)).catch(err => reject(err));
});
};
I was reading about the .bind() function which works for functions but not for promises according to this question. I also saw something about the .apply() function, but this doesn't seems to be what I need.
Edit
My function is invoked like myFirstPromise when there aren't parameters, and like mySecondPromise when I have at least one:
var executePromises = (myFirstPromise, mySecondPromisePromise) => {
return new Promise((resolve, reject) => {
//retrieve an array of every promise status
var promisesStatus = getPromisesStatus();
executeMappingStep(promisesStatus[0], myFirstPromise, null).then(result => {
return convertToXml(result);
}).then(result => {
var parameters = {
value1 : result,
value2: [1,2,3],
value3: "some string",
value4: 10
};
return executeMappingStep(promisesStatus[1], mySecondPromisePromise, parameters);
}).then(result => resolve(result)).catch(err => reject(err));
});
};
var myFirstPromise = function() {
return new Promise(function (resolve, reject) {
//some logic for first promise
});
}
var mySecondPromise = function(firstParam, secondParam, thirdParam, fourthParam) {
return new Promise(function (resolve, reject) {
//some logic for second promise with its params
});
}
Probably, in the function addArrayOfParameters I need to loop into every property on the object (I realized that an array would not work), like this
Is there a way to programmatically add parameters from an array before its resolved?
Edit 2
The reason behind this logic is much complex, but this is pretty much what I need to solve. Basically, in my code, every promise can be executed with errors and there's a logic involved in the executePromises function which returns the status of every promise whether was successfully executed or not. This logic involves more than 10 promises chained, and if some of them fail e.g: promise number 5, then 6,7 etc, will have to be invoked again in some admin backend.
I don't what to replicate the logic for each promise, so that's why I wanted to create the executeMappingStep function to encapsulate that logic.
Probably is hard to explain, I tried to simplify the most as I could. I made a few more changes to explain what I just said.

Probably, I have complicated things a lot here, but anyway, this is the solution that I wanted.
It's good to know that the .then function can invoked in other place and the promises can be created with the needed arguments and then sent as parameter.
With that said:
var executePromises = (myFirstPromise, mySecondPromisePromise) => {
return new Promise((resolve, reject) => {
//retrieve an array of every promise status
var promisesStatus = getPromisesStatus();
executeMappingStep(promisesStatus[0], myFirstPromise()).then(result => {
return convertToXml(result);
}).then(result => {
var mySecondPromisePromiseParameter = mySecondPromise(result, [1,2,3], "some string", 10);
return executeMappingStep(promisesStatus[1], mySecondPromisePromiseParameter);
}).then(result => resolve(result)).catch(err => reject(err));
});
};
An then, the executeMappingStep just receives the promise and invokes the .then internally, this way:
var executeMappingStep = (promiseStatus, myPromise) => {
return new Promise((resolve, reject) => {
//if the execution was success, there's no need to invoke myPromise again
if(promiseStatus.success == true)
return resolve(promiseStatus.previousResponseSaved);
myPromise.then(result => resolve(result)).catch(err => reject(err));
});
};

Probably use async library may resolve your issue. Try to run code on your console and look into that.
With async library:
var async = require("async");
async.waterfall([
function (callback) {
var methodParam = [1, 2, 3];
console.log("first method called");
callback(null, methodParam);
},
function (methodParams, callback) {
var newParams = [4, 5, 6];
newParams = methodParams.concat(newParams);
console.log("second method called", methodParams);
callback(null, newParams);
}
], function (err, res) {
if (err) {
console.log("error occured");
} else {
console.log("final result", res);
}
});
With promise:
var promise1 = (() => {
return new Promise((resolve, reject) => {
var methodParam = [1, 2, 3];
console.log("first promise called");
resolve(methodParam);
});
});
var promise2 = ((methodParams) => {
return new Promise((resolve, reject) => {
var newParams = [4, 5, 6];
newParams = methodParams.concat(newParams);
console.log("second promise called", methodParams);
resolve(newParams);
});
});
promise1()
.then((resPromise1) => {
return promise2(resPromise1);
}).then((resPromise2) => {
console.log("final result", resPromise2);
});

Related

Put Javascript Promise into Functions

I have two promises that I am resolving with promise.all:
var mictest1 = new Promise((resolve, reject) => {
resolve(true);
});
var mictest2 = new Promise((resolve, reject) => {
resolve(true);
});
Promise.all([mictest1, mictest2]).then(data => {
console.log("test passed: " + data);
})
I would like to put the promises mictest1 and mictest2 into a function called mictest() so it does the following:
mictest();
Promise.all([mictest1, mictest2]).then(data => {
console.log("test passed: " + data);
})
In this way I can call the function at will, and when the promises get complicated, i don't have that block of text in front of promise.all
Maybe you're looking for the mictest function to return the Promise.all?
const mictest = () => {
var mictest1 = new Promise((resolve, reject) => {
resolve(true);
});
var mictest2 = new Promise((resolve, reject) => {
resolve(true);
});
return Promise.all([mictest1, mictest2]);
};
mictest().then((data) => {
console.log('test passed:', data);
});
I think you are looking for a function that returns the promise:
function mictest() {
return new Promise((resolve, reject) => {
resolve(true);
});
}
You'd use it like
var mictest1 = mictest();
var mictest2 = mictest();
Promise.all([mictest1, mictest2]).then(data => {
console.log("test passed: " + data);
})
or simply
Promise.all([mictest(), mictest()]).then(data => {
console.log("test passed: " + data);
})
Not quite the way you imagined it but you can get very close:
let promises = mictest();
Promise.all(promises).then(data => {
console.log("test passed: " + data);
})
That's just changing two lines of your imagined code. The implementation is simple:
function mictest () {
return [
new Promise((resolve, reject) => {
resolve(true);
}),
new Promise((resolve, reject) => {
resolve(true);
})
]
}
A promise is a value just like strings, numbers, arrays etc. You can treat it like any value. It just happens to be an object that has a .then() method and is awaitable
Note: actually, any object with a .then() method is awaitable even your own custom created non-promise object (actually any object with a .then() method is a promise even though it is not a Promise)

How can I call series of Promise functions?

The requirement is finishing the current function before moving to the next call:
var current_data = something;
Run(current_data).then((data1) => {
Run(data1).then(data2 => {
Run(data2).then(data3 => {
// and so on
})
})
});
The example above is only possible if I know exactly how much data I want to get.
In order to make the nested promises part of promise chain, you need to return the nested promises.
Run(current_data).then((data1) => {
return Run(data1).then(data2 => {
return Run(data2).then .....
});
});
I'm gonna assume your data is paginated and you don't know how many pages there are, therefore you can use a while loop with await inside of an async function like so:
(async function() {
var currentData = someInitialData;
// loop will break after you've processed all the data
while (currentData.hasMoreData()) {
// get next bunch of data & set it as current
currentData = await Run(currentData);
// do some processing here or whatever
}
})();
You can use the async-await to make code more readable.
async function getData(current_data){
let data1 = await Run(current_data)
let data2 = await Run(data1);
let result = await Run(data2);
return result;
}
Calling the getData function
getData(data)
.then(response => console.log(response))
.catch(error => console.log(error));
Try to avoid nested promises. If you need to call a series of promises, which depend on the previous call's response, then you should instead chain then like the following following -
const promise1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 1000);
});
promise1.then((response) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(response + ' b');
}, 1000);
});
}).then((responseB) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(responseB + ' c');
}, 1000);
});
}).then((responseC) => {
console.log(responseC); // 'foo b c'
})
if your code can support async-await then what Mohammed Ashfaq suggested is an alternative.
If you are executing the same function over and over again but on different data, I would make a recursive function that returns return a Promise.
I just look at my example below using an an array of numbers, you can edit it to your current case.
var current_data = [1,2,4,5,6]
function Run(data){
if(data.length === 0)
return Promise.resolve(data);
return new Promise((resolve, reject)=>{
//your async operation
//wait one second before resolving
setTimeout(()=>{
data.pop()
console.log(data)
resolve(data)
},1000)
})
.then((results)=>{
return Run(results)
})
}
Run(current_data)

How to pass a variable from a Request as the return value of the global function js

Hello and thank you in advance. I'm using npm tedious package to interact with a database. I'm also using Meteor.call and methods, for which I need to pass a variable (newdata in the example below) that carries the data taken from the database as the return value of the function "rr", so that I would be able to use the result of the function in the client by a Meteor call.
function rr(){
var newdata = [];
var ahora = new Request("SELECT * FROM prueba", function (err) {
if (err) {
console.log("err1");
} else {
}
})
ahora.on('row', function(columns) {
columns.forEach(function(column) {
newdata.push(column.value);
});
});
}
I want "newdata" to be the result of the rr function. How can I do that? If I write "return newdata" it's undefined, I can't use await because newdata is not the return value of any function...
Thank you very much.
Tedious doesn't seem to support promises natively, but you can wrap your function in a promise:
function rr() {
return new Promise((resolve, reject) => {
var ahora = new Request("SELECT * FROM prueba", function (err) {
if (err) {
reject(err);
}
});
var newdata = [];
ahora.on('row', function(columns) {
columns.forEach(function(column) {
newdata.push(column.value);
});
});
resolve(newdata);
}
}
Or slightly shorter:
function rr() {
return new Promise((resolve, reject) => {
new Request("SELECT * FROM prueba")
.on("error", reject)
.on("row", function(columns) {
resolve(columns.map(column => column.value))
});
}
}
If you'd rather not make promises manually, you can try Bluebird's promisify function. I also found a tedious-specific promisifying package tedious-promises, but it doesn't seem to be properly maintained.
You could do something like this:
function rr(){
return await new Promise((resolve, reject) => {
new Request("SELECT * FROM prueba", (err, rowCount) => {err && reject(err);})
.on('row', columns => resolve(columns.map(c => c.value)));
});
}

Nested Promises, And Another After Completion [duplicate]

I have a situation where I think the only choice for me is to nest some Promises within each other. I have a Promise that needs to be performed and a method that does something until that Promise is complete. Something like this:
let promise = new Promise((resolve, reject) => {
// Do some stuff
});
doSomethingUntilPromiseisDone(promise);
However, within my Promise, I need to execute another method that returns another Promise:
let promise = new Promise((resolve, reject) => {
fetchValue(url)
.then((value) => {
// Do something here
}).catch((err) => {
console.error(err);
});
});
doSomethingUntilPromiseisDone(promise);
But now, in the fetchValue method's then statement, I have another method I need to execute that, guess what, returns another Promise:
let promise = new Promise((resolve, reject) => {
fetchValue(url)
.then((value) => {
saveToCache(value)
.then((success) => {
console.log('success!!');
resolve('success');
});
}).catch((err) => {
console.error(err);
});
});
doSomethingUntilPromiseisDone(promise);
So in the end, I have a Promise, within a Promise, within a Promise. Is there someway I can structure this better so that it is more straightforward? It seems like nesting them within each other is counter to Promise's intended chaining approach.
Use .then()
let doStuff = (resolve, reject) => {/* resolve() or reject() */};
let promise = new Promise(doStuff);
doSomethingUntilPromiseisDone(
promise
.then(value => fetchValue(url))
.then(value => value.blob())
.then(saveToCache)
)
.then(success => console.log("success!!"))
.catch(err => console.error(err))
you can use generator to flatten your nested promises (Bluebird.couroutine or Generators)
//Bluebird.couroutine
const generator = Promise.coroutine(function*() {
try {
const value = yield fetchValue(url);
const success = yield saveToCache(value);
console.log('success:', success);
} catch(e) {
console.error(err);
}
}));
generator();
Each function will call the next one with the result of the method before.
var promises = [1,2,3].map((guid)=>{
return (param)=> {
console.log("param", param);
var id = guid;
return new Promise(resolve => {
// resolve in a random amount of time
setTimeout(function () {
resolve(id);
}, (Math.random() * 1.5 | 0) * 1000);
});
}
}).reduce(function (acc, curr, index) {
return acc.then(function (res) {
return curr(res[index-1]).then(function (result) {
console.log("result", result);
res.push(result);
return res;
});
});
}, Promise.resolve([]));
promises.then(console.log);

Look for Promise bluebird code review for node.js

When and where need to use new Promise(Function<Function resolve, Function reject> resolver) -> Promise
My Sample code:
userInfo.js
var Promise = require('bluebird');
var winston = require('winston');
var _ = require('lodash');
var request = Promise.promisify(require("request"));
exports.getWeather = function (data) {
var cityName = data.userProfile.city;
return request("http://0.0.0.0:3003/api/Weather/byCity?city=" + cityName).spread(function (res, body) {
var result = JSON.parse(body).data;
return _.merge(data, result);
});
};
exports.getUserProfile = function (userId) {
return new Promise(function (resolve, reject) {
request("http://0.0.0.0:3003/api/UserProfile/getUserProfile?id=" + userId).spread(function (res, body) {
var result = JSON.parse(body).data;
resolve(result);
});
})
};
exports.getEvents = function (data) {
var cityName = data.userProfile.city;
return request("http://0.0.0.0:3003/api/Events/byCity?city=" + cityName).spread(function (res, body) {
var result = JSON.parse(body).data;
return _.merge(data, result);
});
};
exports.getFashion = function (data) {
var gender = data.userProfile.gender;
return request("http://0.0.0.0:3003/api/Fashion/byGender?gender=" + gender).spread(function (res, body) {
var result = JSON.parse(body).data;
return _.merge(data, result);
});
};
exports.displayDetail = function (data) {
console.log(data);
};
Above code I try call in 2 way in promise
getUserProfile.js
var userInfo = require('./userInfo');
module.exports = function(){
return userInfo.getUserProfile(3)
.then(userInfo.getFashion)
.then(userInfo.getEvents)
.then(userInfo.getWeather)
.then(userInfo.displayDetail)
.catch(function (e) {
console.log('Error:');
console.error(e.stack)
})
.finally(function () {
console.log('done');
});
}
2nd way:
getUserInformation.js
var userInfo = require('./userInfo');
module.exports = function () {
return new Promise(function (resolve, reject) {
resolve(3);
})
.then(userInfo.getUserProfile)
.then(userInfo.getFashion)
.then(userInfo.getEvents)
.then(userInfo.getWeather)
.then(userInfo.displayDetail)
.catch(function (e) {
console.log('Error:');
console.error(e.stack)
})
.finally(function () {
console.log('done');
});
};
getDetails.js
var userInfo = require('./getUserInformation');
userInfo()
.then(function(){
console.log('getDetails done')
})
.catch(function (e) {
console.log('Error:');
console.error(e.stack)
})
.finally(function () {
console.log('done');
});
please let me know what the difference and is there any issues by using these way?
exports.getUserProfile = function (userId) {
return new Promise(function (resolve, reject) {
request("http://0.0.0.0:3003/api/UserProfile/getUserProfile?id=" + userId).spread(function (res, body) {
var result = JSON.parse(body).data;
resolve(result);
});
})
};
Please don't do this. Just return from the callback, and return the promise created by then, like you have done it in your other three methods.
return userInfo.getUserProfile(3)
.then(…)
vs.
return new Promise(function (resolve, reject) {
resolve(3);
})
.then(userInfo.getUserProfile)
.then(…)
Well, the first one is much more readable and concise. They're pretty much equivalent except for the case that getUserProfile does throw synchronously, which it shouldn't anyway. Also in the first case getUserProfile is invoked as a method on userInfo, while in the second case it's just a callback function, the this in the calls will be different.
The second pattern can be tremendously simplified though by using Promise.resolve instead of the new Promise constructor:
return Promise.resolve(3)
.then(userInfo.getUserProfile)
.then(…)
This is totally fine, and aligns better with the rest of the chain. Speaking of which, …
.then(userInfo.getFashion)
.then(userInfo.getEvents)
.then(userInfo.getWeather)
where each of the functions returns a promise that resolves with
additional data merged into its argument
is not exactly the best way to solve this. Yes, it ensures that these three functions are called after each other, and is an acceptable pattern for that case. However, in your case you're mixing the request calls to the API with that argument-extraction and result-merging in the same function; which by the separation of concerns you shouldn't. Rather make the functions pure
exports.… = function (arg) {
return request("http://0.0.0.0:3003/api/…?…=" + arg).spread(function (res, body) {
return JSON.parse(body).data;
});
};
And now you can combine them separately - and not only in sequence, but also in parallel:
userInfo.getUserProfile(3)
.then(function(data) {
var p = data.userProfile;
return Promise.prop({
userProfile: 0,
fashion: userInfo.getFashion(p.gender), // `\
events: userInfo.getEvents(p.city), // }=> execute requests in parallel
weather: userInfo.getWeather(p.city) // ./
});
})
.then(userInfo.displayDetail)
.catch(function (e) {
console.error('Error:', e.stack)
});
The first way is much more readable, and there's no benefit to starting the chain with a promise that returns a constant as in your second way.
They both do effectively the same thing, with one caveat: In your second example (Starting the chain with a Promise), the getUserProfile call will be run on the next tick (Similar to if you'd thrown it in a setTimeout 0) rather than atomically.

Categories

Resources