Skip to content

Commit 81c702f

Browse files
Add async: false to can-ajax options
1 parent 618e0d8 commit 81c702f

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

can-ajax-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ if (hasLocalServer) {
5050
done();
5151
});
5252
});
53+
54+
QUnit.test("synchronous get request", function(assert) {
55+
var done = assert.async();
56+
var ok = true;
57+
ajax({
58+
type: "get",
59+
url: __dirname+"/can-ajax-test-result.json",
60+
async: false,
61+
success: function(){
62+
assert.ok(ok, "Callback happens before returning.");
63+
}
64+
}).then(function(){
65+
assert.ok(!ok, "Promise resolves after returning");
66+
done();
67+
});
68+
69+
ok = false;
70+
});
5371
}
5472

5573
QUnit.test("added to namespace (#99)", function (assert) {

can-ajax.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ function ajax(o) {
140140
return canReflect.assignDeep(a,b);
141141
});
142142

143+
var async = o.async !== false;
144+
143145
// Set the default contentType
144146
if(!o.contentType) {
145147
o.contentType = o.type.toUpperCase() === "GET" ?
@@ -202,7 +204,7 @@ function ajax(o) {
202204
if (!isPost && o.data) {
203205
url += "?" + (isJsonContentType ? JSON.stringify(o.data) : param(o.data));
204206
}
205-
xhr.open(type, url);
207+
xhr.open(type, url, async);
206208

207209
// For CORS to send a "simple" request (to avoid a preflight check), the following methods are allowed: GET/POST/HEAD,
208210
// see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests

0 commit comments

Comments
 (0)