In order to make this question as useful to as many people as possible, I will exclude my specific implementation details beyond that fact that I am using the Bluebird promise library with Node + Express below.
So, let's say that I have the following chain (where P returns a promise, and res is the Express HTTP response object):
P().then(function(){
// do nothing if all went well (for now)
// we only care if there is an error
}).catch(function(error){
res.status(500).send("An error occurred");
}).then(function(){
return P();
}).then(function(pVal1){
return [pVal1, P()];
}) // TODO: catch an error from P() here and log pVal1
.spread(function(pVal1, pVal2){
if(pVal1 === pVal2) {
console.log("Success!");
} else {
console.log("Failure");
}
});
Where I have placed the TODO comment above is where I would like to catch an error that might occur from my call to P. If I do catch an error, I would like to log pVal1 and then send a 500 error, as is done in the first catch. However, I am not sure if this is possible with how I am structuring my chain.
I believe that I need to do some "branching," but I do not think that I understand this concept well enough to stop the asynchronous nature of JavaScript from getting the best of me! As such, any help is thoroughly appreciated.
Don't forget to catch errors in the end of the chain. That's also the place to send the response.
Catching errors in the middle of a chain is for intermittent error handling; the chain continues to run, so don't send a response just yet.
Here is something to try it out:
// example middleware
function handle(req, res, next) {
log("----------------");
return async("p1", "foo").then(function (pVal1) {
return pVal1;
}).then(function (pVal1) {
var p2a = async("p2a", "bar"),
p2b = async("p2a", "bar").catch(function (error) {
log("Logging: " + error + " (pVal1 " + pVal1 + ")");
});
return [p2a, p2b];
}).spread(function (pVal1, pVal2) {
if (pVal1 === pVal2) {
res.send("Success!");
} else {
res.send("Failure");
}
}).catch(function (error) {
res.status(500).send("An error occurred");
log("Logging: " + error);
});
}
// ---------------------------------------------------------------------
// mockup response object
var res = {
status: function (code) {
log("Sending status: " + code);
return this;
},
send: function () {
log("Sending response: " + [].join.call(arguments, " "));
return this;
}
};
// mockup promise generator
function async(name, value) {
return new P(function (resolve, reject) {
if ( confirm("let " + name + " succeed?") ) {
log(name + " succeeds...");
resolve(value);
} else {
log(name + " fails...");
reject(name + " has failed");
}
});
}
function log() {
var msg = document.createElement("DIV");
msg.textContent = [].join.call(arguments, " ");
document.getElementById("log").appendChild(msg)
document.body.scrollTop = document.body.scrollHeight;
}
button {
position: fixed;
top: 5px;
}
<script src="http://cdnjs.cloudflare.com/ajax/libs/bluebird/2.9.33/bluebird.min.js"></script>
<button onclick="handle(null, res, null)">Go</button>
<div id="log"></div>
This is possible if you use the explicit Promise.all instead of returning an array into .spread.
}).then(function(pVal1){
// this becomes a `Promise.all` - the aggregation is explicit
var all = Promise.all([pVal1, P()]);
all.catch(function(e){ // "branching", we both return and `catch` the promise
console.log("Error, pVal1 is", pVal1);
});
return all; // return it
}).spread(function(pVal1, pVal2){
// ....
});
Related
I'm currently trying to code a discord bot using node.js. Currently, I have a function that returns a value which a variable is set to. Afterwards, the console prints out the variable. However, the variable is printed out on the console before it gets assigned the value from the function.
The code:
if(commands[0] === "getlevel"){
let region = commands[1] + "1";
let username = commands[2];
var summonerLevel;
console.log(region, username);
API.setRegion(region);
summonerLevel = await API.getSummonerLevel(username);
console.log("HI");
console.log(summonerLevel);
message.channel.send(summonerLevel);
}
});
So what happens is that the console prints about summonerLevel before the API.getSummonerLevel(username) function executes. How would I make it so that the getSummonerLevel() function runs before console.log()?
Code for function:
RiotAPI.prototype.getSummonerLevel = function(_username){
var summonerJSON; var summoner;
var url = baseURL1 + region + baseURL2 + "/lol/summoner/v3/summoners/by-name/" + _username + "?api_key=" + API_KEY;
console.log("Before");
var options = {
uri: url,
simple: false
};
request.get(options)
.then(function(body){
console.log("After");
summonerJSON = body;
summoner = JSON.parse(summonerJSON);
console.log(summoner);
console.log(summoner.summonerLevel);
return summoner.summonerLevel;
})
.catch(function(err){
//should.throw.error.to.console();
})
.catch(function(err){
console.log(err);
});
}
Also, another problem I have is I get an unhandled promise rejection warning and the .catch() that comes after request.get(...) doesn't seem to solve it. How would I fix that was well?
Console log (not sure if this is useful but...):
Before
HI
undefined
(node:13624) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): DiscordAPIError: Cannot send an empty message
(node:13624) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
After
{ id: 45932456,
accountId: 206692908,
name: 'fire263',
profileIconId: 911,
revisionDate: 1511729881000,
summonerLevel: 36 }
36
You have some issues on your code.
Code for function:
RiotAPI.prototype.getSummonerLevel = function(_username){
var summonerJSON; var summoner;
var url = baseURL1 + region + baseURL2 + "/lol/summoner/v3/summoners/by-name/" + _username + "?api_key=" + API_KEY;
console.log("Before");
var options = {
uri: url,
simple: false
};
return request.get(options) // you need to return the promise
.then(function(body){
console.log("After");
summonerJSON = body;
summoner = JSON.parse(summonerJSON);
console.log(summoner);
console.log(summoner.summonerLevel);
return summoner.summonerLevel;
})
.catch(function(err){ // you should only use `.catch` once
console.log(err);
});
}
As jaromanda x mentioned, you need to return the request.get call which in itself is a promise.
But also you shouldn't use multiple .catch calls because if you do it and you do not handle it correctly (read: continue the rejection chain) it won't go to the second .catch call that you had.
You can read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
More specifically this part: "The Promise returned by catch() is rejected if onRejected throws an error or returns a Promise which is itself rejected; otherwise, it is resolved."
Hopefully this will give you enough information to progress on your issue.
Your getSummonerLevel does not return a promise, which is necessary to be awaitable. The easiest way you can achieve that is to make it async as well:
RiotAPI.prototype.getSummonerLevel = async function(_username) {
var url = baseURL1 + region + baseURL2 + "/lol/summoner/v3/summoners/by-name/" + _username + "?api_key=" + API_KEY;
console.log("Before");
var options = {
uri: url,
simple: false
};
try {
var body = await request.get(options);
console.log("After");
var summonerJSON = body;
var summoner = JSON.parse(summonerJSON);
console.log(summoner);
console.log(summoner.summonerLevel);
return summoner.summonerLevel;
} catch (err) {
//should.throw.error.to.console();
console.log(err);
}
};
I am stuck with new challenge on Promises.
Goal: Update the DB entry only if P_KEY exists.
current db is exposed through module and module has get and put method for db. Both returning Promise.
Approach:
API calls for update method handler on node js with ID and Set of values (json)
In handler for post method call to get method of db module check if value on promise success is empty or not if yes return false else true.
If true; data exists call to put method of db module.
but somehow data it always returns false. even if db entry has been made through db api.
/** Function to check if p_key exist*/
function checkIfPKExists(idVal){
pkdb.get(idVal).then(function(value){
if(value){
return true;
} else {
return false;
}
},
function(err){
console.log(err);
return false;
})
}
/** UPDATE METHOD **/
var ch = checkIfPKExists("p_k"+req.body.id);
if(!ch){
res.send("pk does not exist oo " + req.body.id);
} else {
var pk_promise = pkdb.put("p_k"+req.body.id, req.body.pk);
pk_promise.then(
function(){
res.send(JSON.stringify(req.body.pk) + "Updated Successfully");
},
function(err){
res.send("Error occurred : " + err);
}
)
}
My understanding is ch value is set from checkPK function and since thats a promise it just goes ahead and processes if loop which by default stands true and done irrespective whether element is there or not same result. Not found.
What can I do to correct it?
One issue is that no value is returned from checkIfPKExists() function call, see Why is value undefined at .then() chained to Promise?. Use .then() and .catch() to get the Promise value returned from the function
function checkIfPKExists(idVal) {
// `return` the `Promise` here
return pkdb.get(idVal).then(function(value) {
if (value) {
return true;
} else {
return false;
}
}, function(err) {
console.log(err);
return false;
})
}
/** UPDATE METHOD **/
var ch = checkIfPKExists("p_k" + req.body.id);
ch.then(function(bool) {
// if `true` do stuff
if (bool) {
var pk_promise = pkdb.put("p_k" + req.body.id, req.body.pk)
return pk_promise.then(function() {
return res.send(JSON.stringify(req.body.pk) + "Updated Successfully");
}, function(err) {
return res.send("Error occurred : " + err);
})
} else {
// do other stuff
return res.send("pk does not exist oo " + req.body.id);
})
.catch(function(err) {
// handle error
})
checkIfPKExists() is an asynchronous function, if you want to use ch you would have to use a .then() to get it in a function and then use the value.
function checkIfPKExists(idVal)
{
return pkdb.get(idVal).then(function(value)
{
if(value)
{
return true;}
else
{
return false;
}
}, function(err)
{ console.log(err);
return false;
})
}
/** UPDATE METHOD **/
checkIfPKExists("p_k"+req.body.id).then(function(ch){
if(!ch)
{
res.send("pk does not exist oo " + req.body.id);
}
else
{
return pkdb.put("p_k"+req.body.id, req.body.pk).then(
function()
{
res.send(JSON.stringify(req.body.pk) + "Updated Successfully");
},
function(err)
{
res.send("Error occurred : " + err);
})
}})
I am using nodejs and the library Promises/A+ (Chose this one as it seems to be the most popular) https://www.npmjs.com/package/promise. The problem I am facing is even though the async function is completing as it should, it is completing after the failure statement. So sv + ' No it failed' is always printed before a console.log message (which indicated it was successful) I have in the async method. This console.log message should be printed before as it is inside of the async method. I am stuck why this happening? The promise always runs as it failed even though the async method return as it has succeeded?
My Code:
var promise = new Promise(function (resolve, reject) {
var u = asyncmethod(some_var);
if (u){
resolve(some_var)
}
else{
reject(some_var);
}
});
promise.then(function(sv) {
console.log(sv + ' Yes it worked');
}, function(em) {
console.log(sv + ' No it failed');
});
Problem in your asyncmethod, it should be async function
var promise = new Promise(function (resolve, reject) {
//var u = asyncmethod(some_var); // <-- u is not defined, even if you return result stright, as it's the nature of async
asyncmethod(some_var, function(err, result){ //pass callback to your fn
if(err){
reject(err);
} else {
resolve(result);
}
});
});
promise.then(function(successResponse) { //better name variables
console.log(successResponse + ' Yes it worked');
}, function(errorResponse) {
console.log(errorResponse + ' No it failed');
});
//and simple implementation of async `asyncmethod`
var asyncmethod = function(input, callback){
setTimeout(function(){
callback(null, {new_object: true, value: input});
}, 2000); //delay for 2seconds
}
Notice: as the name implies, this answer considers that asyncmethod is async
You're doing it wrong, did you read any documentation about promises?
First of all, you don't need an extra package, nodejs already includes Promise.
If asyncMethod is a promise, you can do this directly:
var promise = asyncmethod(some_var);
promise.then(function (sv) {
console.log(sv + ' Yes it worked');
}, function (em) {
console.log(sv + ' No it failed');
});
Background: I have a PHP background and this is my first application using MEAN stack.
I need to save a record but before I must to check if there is any record under the same id already saved in the DB.
In PHP I would do something like this:
Once the user clicks "Save":
1) Call the function to check if an entry with that id already exists
2) If it doesnt, call the save function.
In Javascript, I'm getting a little confused with Promises and so on.
Can somebody give me some light here?
Right now, I'm doing the following:
In the save api, I call this function to check if the record already exists in the DB:
recordExists = findTranscationByBill(billId);
function findTransactionByBill(billId){
results = new promise(function(resolve, reject){
Transactions.find({billId : billId},function(err, transactions){
if(err)
reject("Error: "+err);
//console.log(transactions);
resolve(transactions);
});
});
results.then(function(data){
console.log('Promise fullfilled: '+ data);
}, function(error){
console.log('Promise rejected: ' + error);
});
return $results;
}
The problem is that I think I'm not using promise properly, as my variable doesn't get populated (because its Async).
In the console.log I see that the promise is being fulfilled however, the variable returns as [object Object]
I'm stucked with this problem because I don't know if I should carry on thinking as PHP mindset or if there is a different approach used in Javascript.
Thanks in advance!
In my opinion you could just as well use a callback for this, and since MongoDB has a count method, why not use it
function findTransactionByBill(billId, callback){
Transactions.count({billId : billId}, function(err, count){
if (err) {
callback(err, false);
} else {
callback(null, count !== 0);
}
});
}
and to use it
findTransactionByBill(billId, function(err, exists) {
if (err) {
// handle errors
} else if ( ! exists ) {
// insert into DB
}
}
I think the right function is:
function findTransactionByBill(billId){
var results = new promise(function(resolve, reject){
Transactions.find({billId : billId},function(err, transactions){
if(err) {
reject(err);
} else {
if (transactions.length === 0) {
reject('No any transaction');
} else {
//console.log(transactions);
resolve(transactions);
}
});
});
results.then(function(data){
console.log('Promise fullfilled: '+ data);
}, function(error){
console.log('Promise rejected: ' + error);
});
return results;
}
And then use it like this:
recordExists = findTranscationByBill(billId);
recordExists.then(function() {
// resolved, there are some transactions
}, function() {
// rejected. Error or no any transactions found
// may be you need to check reject result to act differently then no transactions and then error
});
I assume you are using mongodb native drive.
I think mongodb doesn't have promise built-in supported. So you have to promisify it by a little help from promise library. Please refer this if you want to use bluebird.
After promisifying, the code should looks like that (using bluebird):
Promise = require('bluebird');
// Promisify...
var _db = null;
var client = MongoClient.connectAsync('mongodb://localhost:27017/test')
.then(function(db) {
_db = db
return db.collection("myCollection").findOneAsync({ id: 'billId' })
})
.then(function(item) {
if (item)
_db.save(item);
})
.catch (err) {
// error handling
}
The above code is not perfect, because it introduced a global var, so the better version may be
Promise = require('bluebird');
// Promisify...
var client = MongoClient.connectAsync('mongodb://localhost:27017/test')
.then(function(db) {
return Promise.prop({
item: db.collection("myCollection").findOneAsync({ id: 'billId' },
db: db
})
})
.then(function(result) {
var item = result.item;
var db = result.db
if (item)
db.save(item);
})
.catch (err) {
// error handling
}
You need to check bluebird to know how to use it. Also they are many other promise libraries like q, when, but all are similar stuff.
I have an array of URLs that I'm using a for loop to call http.get requests. Since this is an async process, I'd like to call done after ALL requests have returned.
Here is my current attempt:
grunt.registerTask('verify', function() {
var done = this.async();
var promises = [];
var urlPrefix = 'http://example.com/';
for(var i = 0; i < deployableFiles.length; i++) {
(function(i) {
var deferred = Q.defer();
promises.push(deferred);
var file = deployableFiles[i];
var path = file.filetype + '/' + getVersionedFileName(file.basename, file.filetype);
http.get(urlPrefix + path, function(res) {
deferred.resolve();
if(res.statusCode === 200) {
grunt.log.oklns(path + ' was found on production server.');
} else {
grunt.log.error('Error! ' + path + ' was not found on production server!');
}
}).on('error', function(e) {
grunt.log.error("Got error: " + e.message);
done();
});
})(i);
}
Q.all(promises)
.done(function() {
// Everything executed correctly
return done();
}, function(reason) {
// There was an error somewhere
return done(false);
});
});
I'm sure it's just me not wrapping my head around the whole async nature of node correctly, but is there anything glaringly obvious to anyone else?
I've searched about using http with the Q library, and it appears it might be required to use Q.nfcall to get this to work. I'm just having trouble seeing WHY I'd have to do that. (I'm not adverse to actually doing that, I'm more curious than anything else)
Thanks!
If this is not a typo, promises.push(deferred) should be pushed the promise promises.push(deferred.promise).
function foo() {
...
return defer.promise;
}
// => foo().then(function() ...);
Q.all([
foo(),
foo(),
...
]).done(function() ...);
Q.all expects an array of promises. https://github.com/kriskowal/q#combination
Q.nfcall is just sugar around that if
working with functions that make use of the Node.js callback pattern, where callbacks are in the form of function(err, result)
https://github.com/kriskowal/q#adapting-node
You should always perform promisification at the lowest level possible. That makes reasoning about concurrency a lot easier.
function getPing(url){
return new Q.Promise(function(resolve,reject){
http.get(url,function(res){
// note this will _not_ wait for the whole request
// but just the headers.
if(res.statusCode === 200) resolve();
else reject();
});
});
}
This would let you do:
grunt.registerTask('verify', function() {
var done = this.async();
var urlPrefix = 'http://example.com/';
var pings = deployableFiles.map(function(file){
var path = file.filetype + '/' +
getVersionedFileName(file.basename, file.filetype);
return getPing(urlPrefix + path);
});
Q.all(pings).then(done).catch(function(reason) {
// There was an error somewhere
// this will happen as soon as _one_ promise rejected
return done(false);
});
});
This can be further shortened by using a better promise library like Bluebird.
You can also do this with async:
var urlPrefix = 'http://example.com/';
async.each(deployableFiles, function(file, cb) {
var path = file.filetype
+ '/'
+ getVersionedFileName(file.basename, file.filetype);
http.get(urlPrefix + path, function(res) {
if (res.statusCode === 200)
grunt.log.oklns(path + ' was found on production server.');
else
grunt.log.error('Error! ' + path + ' was not found on production server!');
cb();
}).on('error', function(e) {
grunt.log.error("Got error: " + e.message);
cb(e);
});
}, function(err) {
// all done
if (err) throw err;
// all successful
});