Cómo sincronizar entre funciones asíncronas con jquery diferido

I have two function and I want to sync between them The first function is asynchronous function. Only when I got result from the first function I want to call to the second function I tried this code

  $.when(update().then(function (data) {
       alert(1);
        $.when(func1(),func2()).then( function(dataFunc1,dataFunc2) {});
         });

  update : function(){

            var dfd = $.Deferred();
             d3.json("test.json", function(json) {  
                    var data = createData()
                    dfd.resolve(data);
                    return dfd.promise();
        }};

the problem that the update is called then it call to d3,json and don't do the logic of the d3,json becusse it is asynchronous function then the alert(1) is raise.

I thought that only when the update function will be finished and I will have data from the d3.json then the alert(1) will raise.

Did I miss something ? I read also on reslove and promise ,Do I need to use reslove only when I need to pass parameters ? When I need to use promise ?

preguntado el 28 de agosto de 12 a las 13:08

3 Respuestas

I stubbed your functions, fixed a few syntax errors, and it works fine:

$.when(update()).then(function (updateData) {
    console.log(updateData);

    $.when(func1(),func2())
    .then(function(dataFunc1, dataFunc2) {
        console.log(dataFunc1 + ' ' + dataFunc2);        
    });
});

http://jsfiddle.net/RBS5V/

The most glaring issue was a syntax error on the first line:

$.when(update()).then(function (data) {
//             ^ missing closing parenthesis

Also, you need to return your promises outside of any callbacks:

update: function(){
    var dfd = $.Deferred();

    d3.json("test.json", function(json) {
        var data = createData()
        dfd.resolve(data);
        // dont return a promise here
    }};

    // return it out here
    return dfd.promise();
}

Respondido 28 ago 12, 14:08

@user1365697 resolve means "tell whoever is waiting for this to be finished that it is done", promise is a token you give to a consumer so they can listen for the resolve. - bebe

Do I need to use both of them ? - user1365697

@user1365697 yes. the function you pass to when should always return a deferred o con una promise object in order for it to work properly. - bebe

do I must to use setTimeout ? - user1365697

@user1365697 no. i use the setTimeout to illustrate an asynchronous delay. - bebe

I'm not sure what you were doing wrong, your code looks like it has syntax errors.

I restrucutured it:

$.when((function () {
    var dfd = $.Deferred();
    d3.json("test.json", function(json) {
        var data = createData()
        dfd.resolve(data);
    });
    return dfd.promise();
}())).then(function (data) {
    alert(1);
    $.when(func1(),func2()).then( function(dataFunc1,dataFunc2) {
        alert(2);
    });
});

Esto debería funcionar.

Respondido 28 ago 12, 13:08

It didn't work it still get to the d3.json code didn't do the logic first the alert(1) raise and just then continue to do the logic of d3.josn - user1365697

your problem is due to the asynchronous nature of d3.json() call, i.e.execution of lines after function call is not wait to complete the d3.json() function. you can fixed this problem by using delay method. use delay and try to halt execution for few second just after json call..

contestado el 15 de mayo de 14 a las 11:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.