Implement the promisify(callbackFunction) function so that it takes a callback-based function and returns a promise-based function.
The promise-based function can be called with one or more arguments. If so, pass them to callbackFunction, and add an argument at the end for the callback. In other words, the promise-based function will always be called with one less argument than the callback-based function can take.
All callback functions will always return an argument, which should be returned by the promise-based function
I put this function on the test side it works, however in the validation it does not work :
const promisify = (callbackFunction) => (...args)=>{
return new Promise((resolve,reject)=>{
callbackFunction(...args,(data)=>{
resolve(data)
})
})
}
for test:
async function test1() {
const promiseBased = promisify(callbackBased);
const result = await promiseBased();
if(result == "Hello") {
console.log("It works with no arguments");
} else {
console.error("It doesn't work with no arguments");
}
function callbackBased(callback) {
setTimeout(() => {
callback("Hello");
}, 100);
plz help
The promise-based function can be called with one or more arguments. If so, pass them to callbackFunction, and add an argument at the end for the callback. In other words, the promise-based function will always be called with one less argument than the callback-based function can take.
All callback functions will always return an argument, which should be returned by the promise-based function
I put this function on the test side it works, however in the validation it does not work :
const promisify = (callbackFunction) => (...args)=>{
return new Promise((resolve,reject)=>{
callbackFunction(...args,(data)=>{
resolve(data)
})
})
}
for test:
async function test1() {
const promiseBased = promisify(callbackBased);
const result = await promiseBased();
if(result == "Hello") {
console.log("It works with no arguments");
} else {
console.error("It doesn't work with no arguments");
}
function callbackBased(callback) {
setTimeout(() => {
callback("Hello");
}, 100);
plz help