0

This is a server side implementation and the code needs to wait execution till data is returned. I cannot use data in console.log implementation but need it in an array of objects to be processed outside of the function block.

I have seen many answers to my question but they all involve console.log. I do not need to write them to a console. I need to use the array outside of the promise scope.

db.collection("star").find().toArray (err,result) -> console.log result

i need to use the result out here. I need the array of objects here. I do not need to write to a log. I need for the execution to wait for the data and return it like this:

stars = db.collection("star").find().toArray (err,result) -> return result

I need it execution to wait and return the array in the stars variable to be processed. This is not a browser situation but server side javascript.

  • 1
    Welcome to Stack Overflow. You already asked this question today and have posted it again. Please don't do that. Your previously posted question as well as this one ( which is the same thing ) essentially asks how to act on the value returned from an asynchronous call. This is why you have been pointed to the extensive answers to the already existing question. Coffeescript / TypeScript, it all ends up as JavaScript and pre-processors **do not** change the basic semantics of how this works. Please look at the existing answers and learn from them. – Neil Lunn Oct 29 '17 at 08:21
  • I did take a look at that and did not find any that covered my question as they were different situations. Mine is trying to replace lowdb calls (synchronous) with mongodb calls (asynchronous callback) without rewriting the application. I was able to finally find through searching other keywords an answer: deasync . This library using their loopWhile function was the answer I needed. Now this caused another issue but was able to fix that by making the require call in the same scope as the loopWhile and the variable it uses to check must be defined after the require statement. – mmontano Nov 21 '17 at 16:47

0 Answers0