I'm trying to use two promise libraries in the same chain, rp is request-promise and dc is my own that checks to see if dates are in order.
我正在尝试在同一个链中使用两个promise库,rp是request-promise,dc是我自己的,它检查日期是否有序。
/*this works fine (checks if date are in correct order)*/
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.catch(console.log)
/*this also works fine (makes a post request)*/
rp(options)
.then(console.dir)
.catch(console.error)
/*This works up until the rp call, how do I write this chain?*/
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log).then(rp(options))
.then(console.log)
.catch(console.log);
3 个解决方案
#1
1
Promises' then
function works by taking in a function. In this code section:
Promises'然后函数通过接受函数来工作。在此代码部分中:
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(rp(options))
.then(console.log)
.catch(console.log);
What you're doing with the second then
call is passing in a function that has already been called with some parameters, and so what's actually being passed to the then
function is the result of the rp(options)
. You notice how all of the console.log
are used without their usual parentheses? This is why.
你用第二个调用做的是传入一个已经用一些参数调用的函数,所以实际传递给then函数的是rp(options)的结果。您注意到如何在没有通常括号的情况下使用所有console.log?这就是为什么。
The fix is to pass in a function with the data you want "bound" to it, but without calling the function yet. The way to do this in JavaScript would be:
修复是传递一个函数,其中包含您想要“绑定”它的数据,但尚未调用该函数。在JavaScript中执行此操作的方法是:
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(rp.bind(null, options))
.then(console.log)
.catch(console.log);
The rp.bind() sort of "saves" the options to be used to later call the rp function. The reason the first argument is null
is because that is the parameter used as the this
variable inside of the function call, which we don't really need (hopefully).
rp.bind()排序“保存”用于以后调用rp函数的选项。第一个参数为null的原因是因为这是在函数调用中用作此变量的参数,我们并不真正需要(希望如此)。
Another fix would be to create a new anonymous function whose specific role is to call rp
with options
:
另一个修复是创建一个新的匿名函数,其特定的作用是使用选项调用rp:
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(function() { return rp(options); })
.then(console.log)
.catch(console.log);
#2
1
Promise.then
expect function
as parameter, otherwise its ignored, as rp(options);
seems to return Promise
, it's resolved value is not in concern.
Promise.then期望函数作为参数,否则忽略,作为rp(options);似乎回归了Promise,它的解决价值并未受到关注。
You should use a function to wrap it, and return the result from calling rp(options)
.
您应该使用函数来包装它,并从调用rp(options)返回结果。
It's also worth note that console.log
returns undefined
, if you're expecting the result from checkIfDateIsAfter
, you should also wrap that, and return the result, so the value can pass to next then.
还值得注意的是,console.log返回undefined,如果你期望checkIfDateIsAfter的结果,你也应该包装它,并返回结果,这样值就可以传递到下一个。
dc.checkIfDateIsAfter(departure_date,return_date)
.then(function(res) {
console.log(res);
// Pass the value which would be logged to next chain
// if it'll be used later.
return res;
}).then(function(res) {
// if the rp has anything to do with the value.
rp(options);
})
.then(console.log)
.catch(console.log);
#3
1
Try
尝试
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(function(){ return rp(options);})
.then(console.log)
.catch(console.log);
#1
1
Promises' then
function works by taking in a function. In this code section:
Promises'然后函数通过接受函数来工作。在此代码部分中:
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(rp(options))
.then(console.log)
.catch(console.log);
What you're doing with the second then
call is passing in a function that has already been called with some parameters, and so what's actually being passed to the then
function is the result of the rp(options)
. You notice how all of the console.log
are used without their usual parentheses? This is why.
你用第二个调用做的是传入一个已经用一些参数调用的函数,所以实际传递给then函数的是rp(options)的结果。您注意到如何在没有通常括号的情况下使用所有console.log?这就是为什么。
The fix is to pass in a function with the data you want "bound" to it, but without calling the function yet. The way to do this in JavaScript would be:
修复是传递一个函数,其中包含您想要“绑定”它的数据,但尚未调用该函数。在JavaScript中执行此操作的方法是:
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(rp.bind(null, options))
.then(console.log)
.catch(console.log);
The rp.bind() sort of "saves" the options to be used to later call the rp function. The reason the first argument is null
is because that is the parameter used as the this
variable inside of the function call, which we don't really need (hopefully).
rp.bind()排序“保存”用于以后调用rp函数的选项。第一个参数为null的原因是因为这是在函数调用中用作此变量的参数,我们并不真正需要(希望如此)。
Another fix would be to create a new anonymous function whose specific role is to call rp
with options
:
另一个修复是创建一个新的匿名函数,其特定的作用是使用选项调用rp:
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(function() { return rp(options); })
.then(console.log)
.catch(console.log);
#2
1
Promise.then
expect function
as parameter, otherwise its ignored, as rp(options);
seems to return Promise
, it's resolved value is not in concern.
Promise.then期望函数作为参数,否则忽略,作为rp(options);似乎回归了Promise,它的解决价值并未受到关注。
You should use a function to wrap it, and return the result from calling rp(options)
.
您应该使用函数来包装它,并从调用rp(options)返回结果。
It's also worth note that console.log
returns undefined
, if you're expecting the result from checkIfDateIsAfter
, you should also wrap that, and return the result, so the value can pass to next then.
还值得注意的是,console.log返回undefined,如果你期望checkIfDateIsAfter的结果,你也应该包装它,并返回结果,这样值就可以传递到下一个。
dc.checkIfDateIsAfter(departure_date,return_date)
.then(function(res) {
console.log(res);
// Pass the value which would be logged to next chain
// if it'll be used later.
return res;
}).then(function(res) {
// if the rp has anything to do with the value.
rp(options);
})
.then(console.log)
.catch(console.log);
#3
1
Try
尝试
dc.checkIfDateIsAfter(departure_date,return_date)
.then(console.log)
.then(function(){ return rp(options);})
.then(console.log)
.catch(console.log);