0

I'm trying to use callback function in node js so I pass variable and callback function like this:

if(true){
await this.serialNumber(customer, async serial => {
console.log(serial); // it's log 43435543
});

// I need to use that serial out of the scope of that function by passing it to this new function
await this.storeinDB(customer, serial);
}

// this is the function where pass variable to callback function
async serialNumber(customer, callback){
const serial = "43435543";
callback(serial);
}

Is there a way to do that?

1 Answer 1

1

Call the other function inside the callback like. Since you're not working with Promise using async/await makes no differece.

if(true){
  this.serialNumber(customer, function (serial) {
    console.log(serial); // it's log 43435543
    this.storeinDB(customer, serial);
  });
}

// this is the function where pass variable to callback function
serialNumber(customer, callback){
  const serial = "43435543";
  callback(serial);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.