Orchestrate has a client for NodeJS. It is based on promises so I should define a function then and a function fail. The first should be triggered when the request is successful and the second one when it fails. But sometimes it triggers both and I don't understand why.
Here is an example of a function that calls both then and fail:
user.checkToken = function (token, db, callback) {
if (token) {
db.get('acces_tokens', token).then(function (res) {
if (new Date(res.body.expire) > new Date()) {
//Token is valid
callback(res.body.user, res.body.expire);
} else {
//Token has expired
callback(false, "Token has expired");
}
}).fail(function (err) {
callback(false, "ERROR");
});
} else {
callback(false, "A token is needed");
}
};
I do not understand it :(
EDIT: It turn out it only does this when I call it from certain functions. This is even weirder.
EDIT 2: I've been doing some debuging. I console.logged the entire promise like this:
console.log(
db.get('acces_tokens', token)
.then(function (res) {
if (new Date(res.body.expire) > new Date()) {
//Token in valid
callback(res.body.user, res.body.expire);
} else {
//Token has expired
callback(false, "Token has expired");
}
}).fail(function (err) {
callback(false, "ERROR");
})
)
And I got this:
{ promise: [Circular],
_isPromise: true,
_successFn: null,
_failFn: [Function],
_scope: [Circular],
_boundArgs: null,
_hasContext: false,
_nextContext: undefined,
_currentContext: undefined }
As you can see _successFn is null. I guess this is the problem but I do not understand why it is null :(