2

The following function is responsible to manage requests in a back-end service

const saveData = (body) => {

    // console.log("in save method body: ", JSON.stringify(body), "\n")
    return new Promise(function(resolve, reject) {
        const {fields, item} = body
        
        pool.query('Select count(1) as record_count from my_table where id= $1', [fields.id], (error, results)=>{
            if(error){
                reject(error)
                return
            }

            if(results && results.rows && results.rows[0].record_count > 0){
                console.log("in update fields are ", fields, "\n")
                updateData(fields).catch(error =>{
                    reject(error)
                    return
                })
            }
            else{
                console.log("in insert fields are \n", fields, "\r\n")
                insertData(fields).catch(error =>{
                    console.log("error is ", error)
                    reject(error)
                    return
                })          
            }            

            insertDataDetail(item).then().catch(error => {
                reject(error)
                return
            })
        })
        resolve("Save is done")
    })
}

insertData(..) returns Promise<QueryResult<R>>

I see console.log output but the function execution does not terminate then, the response seems successful but actually, it is not... What is wrong here?

husnu
  • 354
  • 3
  • 15
  • 2
    You're returning the arrow function that you passed as parameter to `.catch` not the surrounding `foo` function. – yi fan song Nov 18 '20 at 20:51
  • I see that, thank you. But how should I manage this could you post an alternative handling strategy? – husnu Nov 18 '20 at 20:55
  • Does this answer your question? [How to return data from promise](https://stackoverflow.com/questions/37533929/how-to-return-data-from-promise) – Siguza Nov 18 '20 at 20:55
  • 1
    This looks quite like the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it). Please [edit] your question to include the complete code so that we can properly judge what you're trying to do and advise how to do it. – Bergi Nov 18 '20 at 22:13
  • 1
    In all probability, foo() should return its own promise rather than try to resolve/reject some outer `new Promise()`. In its simplest form, that would be `function foo() { return insertData(fields) }`. An error arising from the `insertDate()` expression would be automatically reflected in the settlement of the returned promise. – Roamer-1888 Nov 19 '20 at 02:51

1 Answers1

2

The problem is that you are always resolving the promise. You have basically this:

const saveData = () => {
  return new Promise((resolve, reject) => {
    // you call the query function which is async

    resolve("Save is done"); // this is always resolving the function before the query runs

  });
}

Instead you need to do this:

const saveData = () => new Promise((resolve, reject) => {
  pool.query('query', [], (err, results) => {
    if (err) {
      return reject(err);
    }

    // whatever else you check
    return reject(new Error('something is not like I want it'));


    // everything is fine, continue
    return resolve(insertDataDetail(item));

  });
  // if you put something here it will run before the query is ran.
});

Mind the return reject | return resolve. Also mind that you can resolve returning another unresolved promise and if it fails it will be caught in the higher level catch from saveData.

yBrodsky
  • 4,981
  • 3
  • 20
  • 31