Javascript 如何确定参数顺序';s通过.bind()添加
前言,我正在使用BlueBird重写从回调到承诺的代码(顺便说一句,BlueBird非常棒),我正在尝试重构我的代码: 我有一个将在不同情况下使用的函数,这个函数:Javascript 如何确定参数顺序';s通过.bind()添加,javascript,promise,bluebird,Javascript,Promise,Bluebird,前言,我正在使用BlueBird重写从回调到承诺的代码(顺便说一句,BlueBird非常棒),我正在尝试重构我的代码: 我有一个将在不同情况下使用的函数,这个函数: var getPackagesWithIndex = function(request, indexName, indexValue){ return new Promise(function(resolve, reject){ Package.query(indexValue).usingIndex(indexName
var getPackagesWithIndex = function(request, indexName, indexValue){
return new Promise(function(resolve, reject){
Package.query(indexValue).usingIndex(indexName).exec(
function(err, data){
if (err) return reject(err);
// Add the request to the payload
data.request_ota = request;
resolve(data);
});
})
}
我正在链接这样的函数以获得所需的结果:
var promote = function(request){
var destination_build = request.params.destination_build;
return sanitizeInput(request
).then(getPackagesWithIndex.bind(undefined, destination_build, 'destination_build')
).then(preparePackages
).mapSeries(updatePackage)
}
当我console.log
查看结果时,参数的顺序似乎混乱了:
request
获得了indexValue
的值,有没有办法保持事情的有序性?的第一个参数(与Bluebird无关,顺便说一句)是函数调用的这个值。尽管如此,争论仍保持有序
因此,当您使用getPackagesWithIndex.bind(未定义,destination\u build,'destination\u build')
作为回调时,最终将调用getPackagesWithIndex
- a
此
未定义的上下文
- 构建的
目的地的请求
'destination\u build'
- 以及带有承诺结果的
索引值
如果刚刚移动了,则在前面添加类似null
的内容;如果您想要另一个不同的顺序,最好使用包含显式调用的函数表达式。GetPackageWithIndex将使用request==request.params.destination\u build调用,indexName==“destination\u build”
和indexValue==在sanitizeInput
函数中返回的承诺的解析值是的,我知道第一个参数是这样的,我想我没有发现绑定的参数是在末尾添加的,而不是在开头添加的,谢谢Bergi和@Jaromanda-X。是的,我最后做了一些事情,比如:。然后(函数(请求){//Data ready cleanned var destination\u build=request.params.destination\u build;return getPackagesWithIndex(请求,'destination\u build',destination\u build)}