-2

Suppose we have a simple function called logData that handles HTTP requests. Suppose, also, that we have another function called logIntoDatabase.

async logIntoDatabase(message) { ... }

async logData(request, response) {
    await logIntoDatabase("something happened");
    response("log successful");
}

Assume that we don't need to be certain that the data was logged onto the database (we don't need to wait for a response from the database). So we make a little optimization:

async logIntoDatabase(message) { ... }

async logData(request, response) {
    logIntoDatabase("something happened");
    response("log successful");
}

is it ABSOLUTLY GUARANTEED that logIntoDatabase is going to execute? (As if we waited it to be completed)

Nicolas Caous
  • 703
  • 5
  • 18
  • I don't understand what you're asking. Yes, in the second example `logIntoDatabase` will still be fired. It's just not going to wait for it to finish before firing `response`. – zero298 Aug 02 '19 at 18:43
  • 1
    Well, the only thing guaranteed there is that `logIntoDatabase` will be called if `logData` is being called. Can there be an error in between the start of `logData` and `logIntoDatabase` call? or even in the `logIntoDatabase` function? Yes. – MinusFour Aug 02 '19 at 18:44
  • @zero298 I'm asking this because in python asyncio that is not the case. So I wanted to know if I could trust the nodejs event loop in this case. – Nicolas Caous Aug 02 '19 at 18:45
  • @MinusFour Assume there is no error (I know, I know, this is impossible, but the crux of the matter is the event loop). – Nicolas Caous Aug 02 '19 at 18:46
  • You don't need to handle the return values of your async functions (be it to regular callbacks or through promise API), just like you don't need to handle the return value of your sync functions. That's up to you. – MinusFour Aug 02 '19 at 18:50
  • But...no guarantee it will resolve. Have to code it accordingly and catch rejected promises – charlietfl Aug 02 '19 at 18:56
  • The function is always executed. The execution is not driven by the promise being awaited. If you want to drop the `await`, you should have a look at [Can I fire and forget a promise?](https://stackoverflow.com/q/32384449/1048572). – Bergi Aug 03 '19 at 21:27
  • Don't understand the downvotes (at least state what is wrong with the question), however @Bergi pointed out a possible duplicate. Thank you for the link! – Nicolas Caous Aug 04 '19 at 00:57

4 Answers4

1

Yes, it is certain that logIntoDatabase is going to execute as long as the function calling it (logData) is also executed.

Note that it will not surely do what you expected it to do: it may throw an error or take a significantly long time to fully execute, although that's an issue related to the content of the function, not if you were to call it synchronously or asynchronously.

Alon L
  • 152
  • 1
  • 5
1

Script inside promise or asynchronous will always be executed unless your application/server force stop or crash.

You can read this article about how asynchronous work. Script still in stack but not blocking other process.

https://blog.bitsrc.io/understanding-asynchronous-javascript-the-event-loop-74cd408419ff

William Gunawan
  • 748
  • 3
  • 8
1

Are promises guaranteed to be executed?

First off, this is a bit of terminology correctness, but promises don't "execute". Asynchronous functions execute and they create promises that help you monitor their completion. You aren't calling a promise here. You're calling a function that returns you a promise.

is it ABSOLUTLY GUARANTEED that logIntoDatabase is going to execute?

Yes, if logData(...) is called, then logIntoDatabase() will always execute as the very first line in that function in both your first and second code examples.

Your method like this:

async logData(request, response) {
    await logIntoDatabase("something happened");
    response("log successful");
}

is essentially equivalent to this (assuming logIntoDatabase() returns a promise and doesn't throw synchronously):

logData(request, response) {
    return logIntoDatabase("something happened").then(() => {
        response("log successful");
    });
}

As such you can see that the very first line to execute inside logData() is your call to logIntoDatabase().


A full replacement for the async function would be this:

logData(request, response) {
    try {
        return Promise.resolve(logIntoDatabase("something happened")).then(() => {
            response("log successful");
        });
    } catch(e) {
        return Promise.reject(e);
    }
}

This covers other misbehaviors such as logIntoDatabase() not returning a promise or throwing synchronously. But, even in this more complete analogy, you can see that logIntoDatabase() is still guaranteed to be called.


In your 2nd code example:

async logData(request, response) {
    logIntoDatabase("something happened");
    response("log successful");
}

You are still guaranteed that logIntoDatabase() will be called. But, this will differ in behavior from your version with await in three ways:

  1. response("log successful") will always be called even if logIntoDatabase() rejects.

  2. response("log successful") will be called BEFORE logIntoDabase() finishes it's asynchronous operations.

  3. The promise returned from logData() will not have any connection to whether logIntoDatabase() resolves or rejects. If neither logIntoDatabase() or resolve("log successful") throws synchronously, then the promise will be resolved with an undefined resolved value. If either does throw synchronously, then the promise returned from logData() will reject with the exception as the reason for the rejection.

So, your second example is analogous to this:

logData(request, response) {
    try {
        logIntoDatabase("something happened");
        response("log successful");
        return Promise.resolve();
    } catch(e) {
        return Promise.reject(e);
    }
}

And, you can see that logIntoDatabase() is still guaranteed to always be called.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

Yes!

Promises in JavaScript are just like promises in real life, once one is made, it is guaranteed to be kept.

Hope this helps

Tronki
  • 9
  • 1
  • 2
    Well, promises aren't "called". They are a notification and monitoring mechanism. Asynchronous functions are called. Those functions may return a promise which is used to monitor their completion. – jfriend00 Aug 02 '19 at 21:33
  • Just in real like, a promise can be broken (rejected). Also there is no guarantee when a promise would be fulfilled. – Bergi Aug 03 '19 at 21:25
  • It was just a joke guys. – Nicolas Caous Aug 25 '19 at 19:34