在我的情况下,如何将参数传递到函数中
How to pass parameter into a function in my case
我正在尝试为我的应用程序建立承诺。我有类似的东西
var test = function(id,companyID) {
return getProduct(id)
.then(getProductName(companyID))
.then(function(obj) {
console.log(obj)
})
}
test('123', '456');
我只能看到一个包含空上下文的对象。但是,如果我将我的代码更改为 no 在getProductName
函数中包含参数和硬编码,例如
var test = function(id,companyID) {
return getProduct(id)
.then(getProductName)
.then(function(obj) {
console.log(obj)
})
}
test('123', '456');
我在控制台中获得了所需的数据.log
我不确定如何将参数传递到链中。知道怎么做吗?多谢!
如果从
处理程序返回承诺,则从匹配.then()
返回的承诺将适应该承诺,从而允许您执行以下操作:
var test = function(id,companyID) {
return getProduct(id)
.then(function(){
return getProductName(companyID)
})
.then(function(productName) {
console.log(productName);
});
}
你试过吗
var test = function(id,companyID) {
return getProduct(id)
.then(function(){
getProductName(companyID)
.then(function(obj) {
console.log(obj);
});
})
}
你有没有试过这样的事情:
var test = function(id, companyID){
return getProduct(id)
.then(function(data){
getproductName(companyID)
.then(function(data){
console.log(obj);
});
});
};
当你不使用getProductName(companyID)
时,它会失败,因为公司ID是未定义的,所以你需要在你的failHandler中使用:
var test = function(id,companyID) {
return getProduct(id)
.then(getProductName)
.then(function(obj) {//doneHandler
console.log(obj)
},function(obj){//failHandler
console.log(obj);//now this gets logged
})
}
test('123', '456');
相关文章:
- 函数未将值作为参数传递
- 如何在HTML元素上创建函数,而不是将元素作为参数传递
- 如何传递函数中的参数
- 将参数传递给函数
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- 传递带有参数的函数
- javascript,将参数传递给函数内部的闭包中的回调
- 将字符串作为参数传递给函数onclick event jquery
- 将修改后的数组作为参数传递给函数
- 代码气味-将布尔控制参数传递给函数
- 将参数传递给函数,同时保留事件处理程序
- 如何使用按钮将参数传递给函数
- 作为参数传递时如何计算函数
- 将对象作为参数传递时的 Javascript 函数作用域
- 将回调作为参数传递给函数
- 在构造控制器、服务等时作为函数参数传递之前列出的角度 JS 变量
- 作为参数传递的函数的异步执行
- 将请求作为参数传递给函数
- 什么时候需要在javascript中的函数中将对象作为参数传递
- Javascript,对象,用参数传递函数作为参数- angular, jquery概念基本误区