简体   繁体   中英

Question regarding returning a null value in a then callback of a promise in firebase cloud functions

So in my cloud function I have this snippet of code:

return users_pending_update.once('value').then((snapshot) => {
    if (snapshot.exists()) {
        return manage(ref_date, zonesYetToBeUpdated, snapshot);
    } else {
        return users_tzs_ref.once('value').then((snapshot) => {
            if (snapshot.exists()) {
                return manage(ref_date, zonesYetToBeUpdated, snapshot);
            } else {
                return null;
            }
        }).catch((error) => {
            console.error("Getting the list of values from the \"users_tzs\" node failed with error: " + error);
        });
    }
}).catch((error) => {
    console.error("Getting the list of values from the \"users_pending_update\" node failed with error: " + error);
});

My question is about the return null statement you can see in the code. The only reason I have it there is because I was getting a warning that not all code paths returned a value. I don't need or care about that path over there. The warning also goes away if I use something like return Promise.reject("text") . Will it be a problem if I leave it as return null and if yes what should I change it to?

Will it be a problem if I leave it as return null and if yes what should I change it to?

This really depends upon the logic of the caller or what you want the logic of the caller to be. When you do return null; there, you are saying that in this particular code path, you want the resolved value of the promise that you've returned from this function to be null . That would be perfectly fine if:

  • The caller isn't using the resolved value at all and this is the type of function that all that really matters to the caller is when the operation succeeds or errors and there is no specific useful resolved value.
  • The caller is checking the resolved value for null or some other falsey value and acting accordingly. For example, some database interfaces will return something like null if you query for something and no results are found. This isn't an error condition, it's just informing you that no search results were found.

As for changing to something like return Promise.reject("text") , that creates a completely different result. That makes this particular code path reject the promise that it returns which will generate a significantly different behavior for the caller. You would typically only do this if this is an error condition and you want to communicate such an error back to the caller and you expect them to handle this error in a .catch() or try/catch (if using await`).

Also, you would not typically reject with a string. Usually you reject with an Error object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM