我有这样的功能:
myFunction(callback){
somePromise
.then(function (returnValue) {
//figure out how to do a multiple arguments here to callback
callback(returnValue); //figure out how to pass the arguments that were passed to myFunction in here in addition to the returnValue of somePromise.
});
}
我希望能够这样称呼它:
myFunction(myCallBack, callbackArg1, callbackArg2);
myFunction(differentCallback, callbackArg1, callbackArg2,callbackArg3, callbackArg4);
myFunction(thirdCallback);
我希望能够允许用户将回调函数传递给myFunction,并使用Promise的返回值使用回调执行,并且传递给myFunction()的任何其他args
我尝试使用callback.apply(),但我似乎无法将参数传递给它,因为我在Promise then()块中超出了范围。
答案 0 :(得分:0)
你可以这样做:
myFunction(callback, arg1, arg2){
somePromise
.then(function (returnValue) {
//figure out how to do a multiple arguments here to callback
callback(returnValue, arg1, arg2);
});
}
myFunction(myCallBack, callbackArg1, callbackArg2);