This is in a node and express API. I'm trying to figure out the best way to handle Promises that resolve, but return a 404 response. I have a method which uses node-fetch to make a POST call to update a row in a database. If an invalid profileId is provided, the Promise will resolve even though the response is a 404. I want to figure out the best way to make sure that any status other than OK is considered an error and handled as such.

The method is defined as:

async function updateUserProfileSocketId(profileId, socketId) {
    const body = { id: profileId, socketId };
    try {
        const response = await fetch(`${MATCH_API}/updateUserProfile`, {
            method: 'post',
            body: JSON.stringify(body),
            headers: { 'Content-Type': 'application/json' },

        if (response.ok) {
            return response;

        throw new Error(`updateUserProfileSocketId Error: unexpected response ${response.statusText}`);
    } catch (err) {
    return null;

And it is being called as such:

// this is a class method
onInit(socket) {
    socket.on('init', (profile) => {
        updateUserProfileSocketId(profile.id, socket.id)
            .then((response) => {
                if (response !== null && response.ok) {
            .catch((err) => {

This gets the job done but I'm not sure if this is the best way to handle this scenario and I'm not sure if this code has any redundancies. I'm from a Java background so looking for someone to help explain the best methods in dealing with such scenarios. Please let me know if you have any suggestions.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.