1

The idea is that I need to run multiple operations against the database, but only if I need to. For instance if there are no items to insert, then there is no insert call.

function dbOperations(params){ functionInsert(params) .then(functionUpdate(params)) .then(functionDelete(params)) ... } 

then I have

function functionInsert(params){ if (params){ //DO a call against DB which returns a promise return knex.insert().transacting(trx); }else{ //if no params, no need to do anything, just let the next .then() fire next function return Promise.resolve() } } 

By having it like that, the code runs fine but when there are no params then I see this warning Warning: .then() only accepts functions but was passed: [object Object] so it's obvious I am doing something wrong.

How should this scenario be handled, when there are no params?

LE: for db access I am using knex. I've edited the functionInsert above.

4
  • 1
    Don't you have to wrap the blocks in then into some closure to make them execute only when then is called (as opposed to calling it immediately and passing the result to then)? something like .then( function(){ functionUpdate(params)}) Commented Mar 9, 2018 at 9:01
  • Looks like functionUpdate and/or functionDelete are returning object instead of function when there are no params. Commented Mar 9, 2018 at 9:01
  • The reason it works when params are present is that functionInsert does then not return a Promise, but a function that returns a Promise. promiseFromORM vs promiseFromORM(). It probably works if you do Promise.resolve instead of Promise.resolve(). Commented Mar 9, 2018 at 9:07
  • @Thillo using it with then(function... generates blocking between operations. Maybe my functions are called in parallel and not one after the other? It is really important to have them run one after another do avoid deadlocks Commented Mar 9, 2018 at 9:30

1 Answer 1

1

The warning itself is explaining. .then expects a function as its argument, but you're passing the result of the function functionUpdate instead.

You may want to wrap the statement(s) in anonymous functions instead, as pointed out by @Thilo in the comments:

function dbOperations(params){ functionInsert(params) .then(() => functionUpdate(params)) .then(() => functionDelete(params)) ... } 
Sign up to request clarification or add additional context in comments.

9 Comments

But then, you also need to fix the if (params) part to return promiseFromORM() instead of promiseFromORM.
@Thilo -- correct. That function should be called to get the promise returned to the chain.
I've added the detail that I am using knex for db calls if that adds any extra info. So return knex.raw('insert into...')
@31piy the problem with running it like you've answered is that I get blocking processes on my server, basically update query fromfunctionUpdate can block functionInsert so to me this looks like they are executing in parallel maybe? I want tthe secon then() to execute only after the previous one completed successfully
@Alin -- The code that I wrote will make the promises to execute one-by-one. Means, functionUpdate will execute after functionInsert and functionDelete will execute after functionUpdate. Isn't this what you want?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.