Javascript async: Difference between revisions
Jump to navigation
Jump to search
Created page with "=Introduction= =Typical Failing Code= <syntaxhighlight lang="javascript"> export function raceCondition() { let xhr = new XMLHttpRequest(); let statuses = []; xhr.open("..." |
No edit summary |
||
Line 1: | Line 1: | ||
=Introduction= | =Introduction= | ||
=Typical Failing Code= | ==Example Code== | ||
===Typical Failing Code=== | |||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
export function raceCondition() { | export function raceCondition() { | ||
Line 31: | Line 32: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
This may fail because it finishes the second request before the first. I.E. we did not wait for the first request before using the second request. | This may fail because it finishes the second request before the first. I.E. we did not wait for the first request before using the second request. | ||
===Moving Second Request to after First Request=== | |||
<syntaxhighlight lang="javascript"> | |||
export function raceCondition() { | |||
let xhr = new XMLHttpRequest(); | |||
let statuses = []; | |||
xhr.open("GET", "http://localhost:3000/ordersStatuses"); | |||
// Success | |||
xhr.onload = () => { | |||
statuses = JSON.parse(xhr.responseText); | |||
let xhr2 = new XMLHttpRequest(); | |||
xhr2.open("GET", "http://localhost:3000/orders/1"); | |||
// Success | |||
xhr2.onload = () => { | |||
const order = JSON.parse(xhr2.responseText); | |||
const description = status.map((t) => { | |||
if (t.id === order.orderStatusId) { | |||
return t.description; | |||
} | |||
})[0]; | |||
setText("Order Status: ${description}"); | |||
}; | |||
xhr2.send(); | |||
}; | |||
} | |||
</syntaxhighlight> |
Revision as of 04:21, 13 August 2020
Introduction
Example Code
Typical Failing Code
export function raceCondition() {
let xhr = new XMLHttpRequest();
let statuses = [];
xhr.open("GET", "http://localhost:3000/ordersStatuses");
// Success
xhr.onload = () => {
statuses = JSON.parse(xhr.responseText);
};
let xhr2 = new XMLHttpRequest();
xhr2.open("GET", "http://localhost:3000/orders/1");
// Success
xhr2.onload = () => {
const order = JSON.parse(xhr2.responseText);
const description = status.map((t) => {
if (t.id === order.orderStatusId) {
return t.description;
}
})[0];
setText("Order Status: ${description}");
};
xhr2.send();
}
This may fail because it finishes the second request before the first. I.E. we did not wait for the first request before using the second request.
Moving Second Request to after First Request
export function raceCondition() {
let xhr = new XMLHttpRequest();
let statuses = [];
xhr.open("GET", "http://localhost:3000/ordersStatuses");
// Success
xhr.onload = () => {
statuses = JSON.parse(xhr.responseText);
let xhr2 = new XMLHttpRequest();
xhr2.open("GET", "http://localhost:3000/orders/1");
// Success
xhr2.onload = () => {
const order = JSON.parse(xhr2.responseText);
const description = status.map((t) => {
if (t.id === order.orderStatusId) {
return t.description;
}
})[0];
setText("Order Status: ${description}");
};
xhr2.send();
};
}