1

I'm pretty new with the whole callback and promises stuff, honestly speaking I don't know how it works internally and currently I'm working with sails and found myself in a problem, I have in my database person and student, when someone tries to create a student the person must be created first, but the method to do so is in another controller, I can call the method in the other controller using sails.controller.person.create() but, how do I make it so that it waits from a response from create before continuing? or there's no need for it and the function who called create from person will wait for a return?

NOTE: it's obvious but I'm planing to make a create on the database on the create method which is asynchronous that's what got me confused whether it'll wait for the return inside of the query's callback or not.

Baek Ryun
  • 100
  • 1
  • 3
  • 14
  • Well do you need any data from the Person? If not then there's no need, if you do then yes you should. – George Apr 11 '17 at 07:26
  • @ George Yes I do need the id from the person to create the relation, so I need to wait for it, the question is, how? – Baek Ryun Apr 11 '17 at 07:27

1 Answers1

0

I'll suggest to spend some time in understanding asynchronous programming. Ref: How do I return the response from an asynchronous call?

Second, don't call a controller method from another controller. Any logic which is used in multiple places, move that to service and call services from controller.
Reason being controller method take (request, response) as arguments and return response when doing multiple creates, response should be returned at end.

Community
  • 1
  • 1
Sangharsh
  • 2,999
  • 2
  • 15
  • 27