I recently started working with promises and found a strange behavior for me. When i give the .then() function a reference to a undefined function it is just skipped and the next then is called.
An example:
var cust = customer({general: { cust_id: 22 }}); // just for testing
req.pool.getConnectionAsync()
.then(cust.del) // cust.del is 'undefined'
.then(function(dbResult) { console.log("dbresult:"); console.log(dbResult); res.status(200).end(); })
.catch(function (e) { console.log(e); res.status(500).end(); });
So what's happening here:
- getConnectionAsync returns a connection which should be given to cust.del
- cust.del is undefined (was a typo by me the correct function would be cust.delete)
- no error is raised instead the next .then function is called with the connection from getConnectionAsync as "dbresult"
- the output of the last then function is the connection object and not a db result object and status 200 is returned to the client
If i change the code to:
req.pool.getConnectionAsync()
.then(function(conn) { cust.del(conn) }) // type error is raised
.then(function(dbResult) { console.log("dbresult:"); console.log(dbResult); res.status(200).end(); })
.catch(function (e) { console.log(e); res.status(500).end(); });
then i get the expected TypeError and the catch function is called.
Is this an expected behavior? Or am I missing something to prevent this? .then(cust.del) is obviously much cleaner code, but since this function is not callable there should be an error.
Regards Phil
Aucun commentaire:
Enregistrer un commentaire