0

I try to access the data that is inside a promise from outside the promise.

If I use the code below for console.log(result), the data appears.

dataModel.lastIndex(name, function(err, result){ console.log(result) });

But, when I try to use return and a variable to save the return results, I can't find the results.

let data1 = dataModel.lastIndex(name, function(err, result){ return result});

console.log(data1);  // doesn't work

How do I return the results that have been promised so that I can use them outside of promise?

Because I have tried to find some of the same questions, but have not been able to solve my problem.

I beg for help.

Thank you in advance.

Titus Sutio Fanpula
  • 3,467
  • 4
  • 14
  • 33
  • Sir,please read about callbacks becuase nodejs is async programming and hence you won't get anything in your code – Atul Agrawal Aug 11 '18 at 06:11
  • I mean, in promise, I can get data using console.log(callback') or whatever variabel I'm use. So, how can I do so that I can display the data inside the promise "with a variable that is outside of the promise?" but, thank you for your response, sir. – Titus Sutio Fanpula Aug 11 '18 at 06:23
  • You can't. First off, there are no promises shown in your code at all. You just show a plain async callback. Second, asynchronous callbacks occur long AFTER your function has already returned so you CANNOT return their value, ever. Instead, you use the value inside the callback. – jfriend00 Aug 11 '18 at 07:45

0 Answers0