0

Here is my code:

Promise.all([
    ajaxRequest(value, 'search/twitter', 'twitter').then(
        function(res) {
            var imgStatus = res[1].length > 0 ? "successfully.png" : "noRes.png";
            $('.option_name_twitter + td').html("<img src='img/" + imgStatus + "' />")

            optionsRes['twitter'] = res[1];
            $('input.twitter').show();

        }, function(err){
            console.log(err);
            $('.option_name_twitter + td').html("<img src='img/warning.png' />")
        }
    ),
    ajaxRequest(value, 'search/instagram', 'instagram').then(
        function(res) {
            var imgStatus = res[1].length > 0 ? "successfully.png" : "noRes.png";
            $('.option_name_instagram + td').html("<img src='img/" + imgStatus + "' />")

            optionsRes['instagram'] = res[1];
            $('input.instagram').show();

        }, function(err){
            $('.option_name_instagram + td').html("<img src='img/warning.png' />")
        }
    )
]).then(() => {
    stopBlinking()
    formSubmited = false;
}).catch( (err) => {
    console.error(err);
    stopBlinking()
    formSubmited = false;
})

As you see I have two functions into promise.all() method. Sometimes I need to call only one of those function separately. Something like this:

$("input.twitter").on('click', function(){
    // only the first function should be called here
})

$("input.instagram").on('click', function(){
    // only the second function should be called here
})

These ^ will be called after when Promise.all() is executed once. Because both input.twitter and input.instagram are hidden at first and will be shown after Promise.all() call.

So I want to initial those anonymous functions (which are exist at promise.all()) into variables, and use them on click of input.instagram and input.twitter. How can I do that?

6
  • Make them not anonymous would be my suggestion Commented Sep 25, 2017 at 5:56
  • into variables var variable = function(){...}. use them on click, $('input').on('click', function(){ if($(this).hasClass(...)) {... }else if( $(this).hasClass(...)){...} }) Commented Sep 25, 2017 at 5:59
  • Are you talking about the functions you are passing to .then or about the ajaxRequest calls? Commented Sep 25, 2017 at 6:00
  • @FelixKling ajaxRequest Commented Sep 25, 2017 at 6:03
  • 1
    Just FYI, ajaxRequest(...) is a function call. "Anonymous function" usually refers to a function definition (without a name). E.g. the functions you pass to .then are anonymous functions. Commented Sep 25, 2017 at 6:10

1 Answer 1

2

Just put the code in two functions?

function searchTwitter() { // maybe specify `value` as argument
  return ajaxRequest(value, 'search/twitter', 'twitter').then(...);
}

function searchInstagram() {
  return ajaxRequest(value, 'search/instagram', 'instagram').then(...);
}

Promise.all([searchTwitter(), searchInstagram()]).then(...);
// and whenever you want:
searchTwitter().then(...);
searchInstagram().then(...);

You can learn more about functions in Eloquent JavaScript.

Sign up to request clarification or add additional context in comments.

10 Comments

use them on click of, this part is missing.
@Rajesh: That's the and whenever you want: part.
@FelixKling Why cannot OP call the function within event handler using current code?
@guest271314: How should that be possible? They didn't define any functions that can be called and I assume they don't want to copy and paste the whole thing.
@guest271314: I guess it's defined somewhere but as I said I assume they don't want to copy and paste the whole function call (including the arguments passed to .then) into the event handlers.
|

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.