I have a number of functions that are written to accept two callbacks and some parameters that I would like to Promisify. Example:
function myFunction(successCallback, failureCallback, someParam)
Given the above function how would I Promisify both the successCallback
and failureCallback
using a Promise library such as Bluebird?
I have tried this but it returns undefined
:
const myFunctionAsync = Promise.promisify(myFunction);
console.log(await myFunctionAsync('someParam')); // undefined
A working but overly verbose solution:
const myFunctionAsync = new Promise((resolve, reject) =>
myFunction(success => resolve(success), failure => reject(failure))
);
console.log(await myFunctionAsync('someParam')); // success
I'm looking for a way to convert these awkward multiple callback functions into Promises without wrapping each one.
Many thanks.
I have a number of functions that are written to accept two callbacks and some parameters that I would like to Promisify. Example:
function myFunction(successCallback, failureCallback, someParam)
Given the above function how would I Promisify both the successCallback
and failureCallback
using a Promise library such as Bluebird?
I have tried this but it returns undefined
:
const myFunctionAsync = Promise.promisify(myFunction);
console.log(await myFunctionAsync('someParam')); // undefined
A working but overly verbose solution:
const myFunctionAsync = new Promise((resolve, reject) =>
myFunction(success => resolve(success), failure => reject(failure))
);
console.log(await myFunctionAsync('someParam')); // success
I'm looking for a way to convert these awkward multiple callback functions into Promises without wrapping each one.
Many thanks.
Share Improve this question asked Oct 12, 2017 at 19:31 RobulaRobula 7161 gold badge13 silver badges29 bronze badges 2- Well you will need to create (wrap) your callbacks in a promise. Whether you do it yourself or a library has a handy utility method it seems to be teh same thing. – Chris Phillips Commented Oct 12, 2017 at 19:38
- True that, maybe I am getting too hung up on the Promisify function which works great for Node style functions where the last parameter is a callback. – Robula Commented Oct 12, 2017 at 19:41
2 Answers
Reset to default 3You could write your own version of a promisify function, that would take that function signature into account:
function myFunction(successCallback, failureCallback, someParam) {
setTimeout(_ => successCallback('ok:' + someParam), 100);
}
function myPromisify(f) {
return function(...args) {
return new Promise( (resolve, reject) => f(resolve, reject, ...args) );
}
}
async function test() {
const myFunctionAsync = myPromisify(myFunction);
console.log(await myFunctionAsync('someParam')); // success
}
test();
Bluebird or otherwise, it's not that hard to promisify functions. Your solution is overly verbose. Try:
const myFunctionAsync = (...a) => new Promise((r, e) => myFunction(r, e, ...a));
Yeah, this is wrapping each one, but with one line per function, unless your functions all follow some pattern and you have them in array, it's not that big a deal. I.e. you're assuming additional args are at the end rather than the beginning.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745217863a4617109.html
评论列表(0条)