Can I fire and forget a promise in nodejs (ES7)?

16,934

Solution 1

Yes, you can do that, and it will run the two asynchronous functions in parallel. You've just created a promise and thrown it away.

However, this means that when the promise is rejected you won't notice. You'll just get an unhandledRejection eventually which will crash your process if not handled.

Is this OK? How can I run something that I don't care?

Probably it's not OK. If you truly wouldn't care, you hadn't run it in the first place. So you should be clear and explicit what you care about (and what not):

  • do you want to wait? (for side effects)
  • do you need the result?
  • do you want to catch exceptions?

If you only want to wait and don't care for the result value, you can easily throw away the result:

void (await someAsyncFunction()); // or omit the void keyword,
                                  // doesn't make a difference in an expression statement

If you don't care about exceptions, you can ignore them using

… someAsyncFunction().catch(function ignore() {}) …

You can throw that away, await it, do anything with it.

If you want the result, you have to await it. If you care about exceptions, but don't really want to wait, you may want to execute it in parallel with the following functions:

var [_, res] = await Promise.all([
    someAsyncFunction(), // result is ignored, exceptions aren't
    someOtherAsyncFunction()
]);
return res;

Solution 2

inside an async function without await the first line. is this OK?

Yes, there are cases where you'd want to do this which are perfectly reasonable. Especially where you don't care about the result - one example is an analytics tracking operation that should not interfere with business critical code.

how else can I run something that I don't care?

In many ways, however simply calling the promise function works. Your del without a callback would probably work in this case but some functions don't guard against not passing callbacks, so you can pass an empty function instead (.del('key', () => {})).

You do want to however make sure that you know about it failing, even if you don't want to disrupt the operation of code - so please consider adding a process.on("unhandledRejection', event handler to explicitly ignore these particular exceptions or suppress them via:

redisClient.delAsync('key').catch(()=>{});

Or preferably, something like:

redisClient.delAsync('key').catch(logErr);
Share:
16,934

Related videos on Youtube

arisalexis
Author by

arisalexis

Full stack developer specialized in Java,Javascript and Graph databases.

Updated on July 15, 2022

Comments

  • arisalexis
    arisalexis almost 2 years

    I would like to run this code with babel:

    redisClientAsync.delAsync('key');
    return await someOtherAsyncFunction();
    

    inside an async function without await the first line. is this OK?

    how else can I run something that I don't care?

    Can I just fire the non-promisified function del('key',null) without a callback?

    • mostruash
      mostruash over 8 years
      It is OK as long as the process that runs it doesn't exit before async call is complete.
    • MinusFour
      MinusFour over 8 years
      Are you using Babel? That's an ES7 feature.
    • arisalexis
      arisalexis over 8 years
      @MinusFour Yes I am using babel
    • arisalexis
      arisalexis over 8 years
      @mostruash it is impossible to know that right? I will just create a race condition
  • arisalexis
    arisalexis over 8 years
    thanks that was comprehensive. If I don't want the result, the error and I don't want to wait what is the best inside an async function. to run the promise or run the normal function (non-promisified) with a null (or function not doing anything) callback?
  • Bergi
    Bergi over 8 years
    In that case you probably can omit the promisification as well. I'd still pass a callback that explicitly does nothing, to show that it's asynchronous but you ignore errors.
  • mylord
    mylord about 7 years
    will the next .then be called if it resolves? I wouldn't want it to.
  • Bergi
    Bergi about 7 years
    @mylord I don't know what you mean. onfulfill callbacks are always called when a promise fulfills.
  • mylord
    mylord about 7 years
    Is there a way to basically use the promise but have it not continue into the .then? Why? Because I don't always want to alter by block/scope structure just to call a function (promise) when I don't care about doing anything in particular after the call.
  • mylord
    mylord about 7 years
    How can I "omit the promisification", basically, when calling a function that calls/returns a promise? Meaning, how can I call it so it just does nothing after being fulfilled? I would like, however, exceptions to be at least printed out, if not handled at any/next .catch.
  • Bergi
    Bergi about 7 years
    @mylord I have no idea what you mean by "alter by block/scope structure". And without then, you cannot really use the promise. Of course, you can just call the function and discard (ignore) the promise it returns - that seems to be exactly what you want. The creation of the promise cannot be omitted, but that's not necessary. You'll get an unhandledRejection on exceptions.
  • mylord
    mylord about 7 years
    @Bergi, the problem is: if you just call the promisified function, any latter next .then() in your promise chain will get called, unintentionally, in this case. How can you call a promisified function and have it "forget" to execute any .then after it resolves?
  • Bergi
    Bergi about 7 years
    @mylord No, no then callback will unintentionally get called when you just don't install any in the first place (including those from chaining or await).
  • mylord
    mylord about 7 years
    @Bergi I should try this again, but won't .then on 9 happen, regardless of whether or not we add return to line 7? hastebin.com/faduqiribu.php
  • Bergi
    Bergi about 7 years
    @mylord if you return on line 7, line 10 will be executed only when the promisifiedFunction() promise fulfills. If you don't, it will be executed when the promise chain (that is completely unrelated to promisifiedFunction() now) fulfills. If you still have a problem with this, you probably should ask a new question with a complete example and description.
  • PRMan
    PRMan over 4 years
    I feel like this answer answers the question much better than the other one.
  • Sunil Patel
    Sunil Patel over 4 years
    Great explanation.