-1

full code here: http://jsfiddle.net/BurFz/ http://jsbin.com/dagequha/1/edit?js,console

/**
 *   Running this will work
 */
func1('arg1').then(func2).then(func3).then(function () {
    console.log('all done!');
});


/**
 *   But this one doesn't work
 */
func1('arg1').then(func2('arg1')).then(func3('arg1', 'arg2')).then(function () {
  console.log('all done!');
});

I have a 3 async functions in my code and I'm using jQuery deferred/promise technique to call them sequentially. It's working all right but the problem is that I can't pass these functions arguments. If you run my JS Bin(JSfiddle) sample you see it works, scroll down and use the second commented section(the one with arguments) instead of first one and it will stop working correctly. How can I pass arguments to func1, func2 and func3 and still call them sequentially?

Iman Mohamadi
  • 6,552
  • 3
  • 34
  • 33
  • Your code belongs within your question. – Kevin B Apr 09 '14 at 16:01
  • sorry @KevinB I don't exactly understand what you mean, do you have any problem opening the link I shared? – Iman Mohamadi Apr 09 '14 at 16:07
  • @Quentin this is definitely a brand new question and the problem is about using "jQuery then" function not passing arguments generally. – Iman Mohamadi Apr 09 '14 at 16:09
  • @ImanMohamadi No, the problem with your code only being in jsbin is if we or anyone who visits this page can't get to jsbin, your question becomes useless to that person. It also may affect search results. – Kevin B Apr 09 '14 at 16:12
  • Also, *"not passing arguments generally"* yes, that is exactly what this question is asking and the solution in the linked question does show how to solve your problem. – Kevin B Apr 09 '14 at 16:14
  • Take it easy @KevinB, the question you linked for was totally targeting something different and made me confuse. Even the answers are the same it's always good to add some details for the questioner just like Michael did for me. – Iman Mohamadi Apr 09 '14 at 16:38
  • That doesn't change the fact that this is a duplicate of the linked question. The fact that you're using .then and they're using something else is irrelevant. – Kevin B Apr 09 '14 at 16:54
  • If I knew the source of my problem, I wouldn't come to stackoverflow in the first place. end of the story, this question is solved! – Iman Mohamadi Apr 09 '14 at 17:19

1 Answers1

1

Try this it would work

$.when(func1('arg1')).then(function(){
    func2('arg1');}).then(function(){
        func3('arg1','arg2');}).then(function () {
             console.log('all done!');
});
Michael
  • 343
  • 2
  • 13