4

I have this code:

async function test() {
    var mongo = await MongoClient.connect(connectionString);
    var db = await mongo.db(databaseName);
    var audit = db.collection(collectionName);
    var result = await audit.find({}).toArray();
    return arrayData;
}

var myData = test();

But apparently the myData is still the promise object. In C# i know there is function GetAwaiter to make it wait for the promise to be finished, is there any equivalent of that in NodeJS? or is there any alternative fort this case? thank you.

Hans Yulian
  • 1,080
  • 8
  • 26
  • 1
    `(async () => {var myData = await test();})();`. That is to say, IIFE. FYI, C# supports `async` `Main`s now :) – Aluan Haddad Jan 27 '18 at 10:10
  • I do not completely understand why do you need GetAwaiter to wait for task to finish - you can do the same thing with Task object (like .Result or await). – Yevgeniy.Chernobrivets Jan 27 '18 at 10:16
  • @Yevgeniy.Chernobrivets `.GetAwaiter().GetResult()` is recommended over `.Result`, if you cannot `await`. IIRC it has to do with exception propagation. But with the ability to write `async Task Main(string[] args)` it isn't needed much these days. – Aluan Haddad Jan 27 '18 at 10:19
  • @Yevgeniy.Chernobrivets without using GetAwaiter, you will need to put a more indentation or create a new function which makes the code harder to read and seems messy – Hans Yulian Jan 27 '18 at 11:49

1 Answers1

2

Nope! All async functions return promises, and you need to either await them from inside another async function, or then them:

So you can either:

(async () => {
    var myData = await test();
})();

or

test().then(data => {
    var myData = data;
});

In other words, there's no way in Node to turn an async function into a sync function. It's async all the way, which by the way is also the recommended approach in C#.


N.B. When you use await on a promise, exceptions are thrown, so you need to encompass your await statement in a try/catch block. When using .then() however, you're expected to also specify a .catch() method to handle errors.

Arash Motamedi
  • 9,284
  • 5
  • 34
  • 43
  • ah man! i know this one and i am hoping that this is not the answer that will come, but i quite expect that this one will come. Ok so far i think this is the only possible way to do – Hans Yulian Jan 27 '18 at 10:28
  • I hear you! I also come from a C# background, and it was sometimes handy to be able to `wait` synchronously on an async operation. But in Node, there's no way around it (and probably for good reason.) My suggestion is to embrace the async and promises approach. You'll soon get used to it. Good luck :) – Arash Motamedi Jan 27 '18 at 10:32
  • i got what you mean, i was about to do the one that you are describing, but i still looking for a possible approach to be able just like C# – Hans Yulian Jan 27 '18 at 10:34
  • I sincerely advise against blocking on an async operation, as this can degrade your application performance and definitely breaks the appropriate pattern and usage of async operations. With the `async/await` keywords, you can easily and neatly perform async operations and maintain a linear code. However, if you really are looking for a way to convert an async operation into sync (again, don't, if possible) look here for an approach/hack: https://stackoverflow.com/questions/21819858/how-to-wrap-async-function-calls-into-a-sync-function-in-node-js-or-javascript – Arash Motamedi Jan 27 '18 at 10:42