Bug Tracker

Opened 12 years ago

Closed 12 years ago

#10598 closed enhancement (invalid)

jXHR.done should be fired with setTimeout when xhr is in complete state

Reported by: Gray Zhang <otakustay@…> Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.6.4
Keywords: Cc:
Blocked by: Blocking:

Description

How to produce

execute code below:

var xhr = $.get('some-resource');
setTimeout(function() {
    var i = 1;
    xhr.done(function() {
        alert(i);
    });
    var i = 2;
}, 500);

What is expected

Since $.get is processed asynchronously, the line var i = 2; should be executed before the alert, so it should alert 2

What is the result

If server responses within 500ms, it alerts 1, otherwise it alerts 2

Comment

This would cause confusion, the same code may emmit different result due to different network latency, so I recommend all callbacks registered to the jXHR object fires with a setTimeout to simulate the async nature of common XMLHttpRequest control flow.

Change History (2)

comment:1 Changed 12 years ago by Gray Zhang <otakustay@…>

Though the documentation said If the request is already complete, the callback is fired immediately, but I preferrer it being changed to produce exactly same result for either condition

comment:2 Changed 12 years ago by dmethvin

Resolution: invalid
Status: newclosed

I preferrer it being changed

We'd need to change the documented interface, so no, sorry. If you need the var i=2 code to always execute after the first .done(), add it in a second .done().

Note: See TracTickets for help on using tickets.