Переменная область действия функции обратного вызова - PullRequest
0 голосов
/ 21 апреля 2019

Я пытаюсь использовать функцию обратного вызова в узле js, поэтому я передаю переменную и функцию обратного вызова следующим образом:

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);
}

Есть ли способ сделать это?

1 Ответ

1 голос
/ 21 апреля 2019

Вызовите другую функцию внутри обратного вызова, как.Поскольку вы не работаете с Promise, использование async/await не делает различий.

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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...