Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Validation doesn't work in custom promisify function

amin

New Coder
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
 

Attachments

  • Capture3.PNG
    Capture3.PNG
    31.4 KB · Views: 1
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
up
 

New Threads

Buy us a coffee!

Back
Top Bottom