0

check_promo is an ajax function and parameter 'param' is from ajax success. How do I want to call that parameter to be processed in the next function?

I tried to display alerts but it did not match the resulting ajax

function result_promo(){
    var data = '';
    check_promo(function(param){ //output param is 1
        data = param;
    });
    return data ;
}

function submit(){
    var check = result_promo();
    alert(check); //not showing 1 but null
}

4 Answers 4

1
function result_promo(){
    var data = '';
    check_promo(function(param){ //output param is 1
        submit(param);
    });
    return data ;
}

function submit(param){
    var check = param;
    alert(check); //It will show 1 in alert box
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use promise to archive this. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Comments

0

Since check_promo is making an AJAX call and the parameter passed to it is a callback, what's happening is that you're firing off the AJAX function but result_promo returns before check_promo to complete. Hence data is null.

Instead, you'd be better off calling check_promo in submit. The result_promo function serves no real purpose here.

4 Comments

so, how should i make
Call check_promo inside submit, the way you do in result_promo. And put the alert in the callback function.
but the result of the callback function I need to do the next process in another function
Then you'll have to pass a callback to that function and pass that up to check_promo. Or you can read about and use (and return) promises.
0

If your function ' check_promo' is used AJAX call then you need to use Promise or Observable for getting the latest res.

For more information, please follow : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

https://rxjs-dev.firebaseapp.com/guide/observable

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.