Wait until a callback ends to continue execution in Angular

Roberto Milani

I'm having some problems understading how the callbacks work. I'm writing a function that has to validate the user's input. Inside the function I have to make an HTTP GET call to my API to perform a check based on the user input.

The problem is that the validate function is called from the process function and submit function is called before the HTTP call that I make inside validate(). I cannot edit process function because it is a function used by other components.

form.process = function(){
     // do stuffs
     validate();
     submit();
}

form.validate = function () {
    // lots of checks regarding the model
    ...
    // HTTP GET call
}

Is it possible to let the submit function waits until the HTTP GET call inside validate() ends?

Thanks in advance :)

Giacomo Mattiuzzi

You MUST modify validate to return a promise like this:

form.validate = function () {
    var deferred = $q.defer();
    // lots of checks regarding the model
    ...
    // In http GET call:
    // If success
    deferred.resolve(<any value>);
    // If errors
    deferred.reject(<any value>);
    // and now return the promise
    return deferred.promise;
}

Now you CAN do anything you want in process function like this:

form.process = function(){
   // do stuffs
   validate().then(function(response){
      submit();
   }, function(reject){
      // do something like showing error.
   });
}

If you have more components that use this function, you MUST edit all like this. Anyway, this is the best way to implement other GET calls in each "validate" function of your components.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wait until callback

From Dev

Wait for cftool to close, then continue execution

From Dev

Wait until a callback address is called

From Dev

jQuery wait until function ends then do it again

From Dev

How to wait until matplotlib animation ends?

From Dev

Marmalade Quick: wait until animation ends

From Dev

jQuery wait until function ends then do it again

From Dev

Async wait until form is loaded to continue

From Dev

Pause an "until" loop and wait for a keypress to continue (Ruby)

From Dev

Pyserial readline() and wait until receives a value to continue

From Dev

jQuery - to wait until function returns and then continue

From Dev

wait for a jbutton click to continue with program execution java

From Dev

wait the execution of foreach to continue next in ionic 2

From Dev

Sequelize wait until loop finished with callback

From Dev

How to wait until Callback has data?

From Dev

Koa.js wait until execution completes

From Dev

Pause execution of a method until callback is finished

From Dev

Hold on callback function execution until promise is completed

From Dev

JS wait for callback to complete execution inside a for loop

From Dev

Make templateHelpers wait until async function ends in Marionette

From Dev

Sequential async task queue does not wait until async task ends

From Dev

Does the SKAction sequence actually wait until the action ends?

From Dev

Wait until zip fully written then continue, Bash Script

From Dev

How to make for loop wait until Async call was successful before to continue

From Dev

How to make for loop wait until Async call was successful before to continue

From Dev

FB Android-SDK: How to wait until callback of request is completed

From Dev

Javascript wait until last event fired in callback before proceeding

From Dev

Why does event handler wait until triggering code completes execution?

From Dev

Command line does not wait until the exe execution is finished

Related Related

  1. 1

    Wait until callback

  2. 2

    Wait for cftool to close, then continue execution

  3. 3

    Wait until a callback address is called

  4. 4

    jQuery wait until function ends then do it again

  5. 5

    How to wait until matplotlib animation ends?

  6. 6

    Marmalade Quick: wait until animation ends

  7. 7

    jQuery wait until function ends then do it again

  8. 8

    Async wait until form is loaded to continue

  9. 9

    Pause an "until" loop and wait for a keypress to continue (Ruby)

  10. 10

    Pyserial readline() and wait until receives a value to continue

  11. 11

    jQuery - to wait until function returns and then continue

  12. 12

    wait for a jbutton click to continue with program execution java

  13. 13

    wait the execution of foreach to continue next in ionic 2

  14. 14

    Sequelize wait until loop finished with callback

  15. 15

    How to wait until Callback has data?

  16. 16

    Koa.js wait until execution completes

  17. 17

    Pause execution of a method until callback is finished

  18. 18

    Hold on callback function execution until promise is completed

  19. 19

    JS wait for callback to complete execution inside a for loop

  20. 20

    Make templateHelpers wait until async function ends in Marionette

  21. 21

    Sequential async task queue does not wait until async task ends

  22. 22

    Does the SKAction sequence actually wait until the action ends?

  23. 23

    Wait until zip fully written then continue, Bash Script

  24. 24

    How to make for loop wait until Async call was successful before to continue

  25. 25

    How to make for loop wait until Async call was successful before to continue

  26. 26

    FB Android-SDK: How to wait until callback of request is completed

  27. 27

    Javascript wait until last event fired in callback before proceeding

  28. 28

    Why does event handler wait until triggering code completes execution?

  29. 29

    Command line does not wait until the exe execution is finished

HotTag

Archive