Kristofer Giltvedt Selbekk
I'm a full stack engineer that just happens to love React, CSS and front end in general
from callback hell
to async heaven
() => {}
function ajax(url, dataReceived) {
const request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE && request.status === 200) {
dataReceived(request.responseText);
}
};
request.open('GET', url);
request.send();
};ajax('/api/user', function(user) {
console.log(user);
});ajax('/api/user', function(user) {
ajax(`/api/user/${user.id}/profile`, function(profile) {
ajax(`/api/posts/${profile.preferenceId}`, function (posts) {
console.log('Preferred posts for user:', posts);
});
});
});ajax('/api/user', function(user) {
ajax(`/api/user/${user.id}/profile`, function(profile) {
ajax(`/api/posts/${profile.preferenceId}`, function (posts) {
console.log('Preferred posts for user:', posts);
}, function(err) {
console.error('Could not load preferences');
});
}, function(err) {
console.error('Could not load profile information');
});
}, function(err) {
console.error('Could not load user data');
});ajax('/api/user', function(user) {
if (user.loggedIn) {
ajax(`/api/user/${user.id}/profile`, function(profile) {
if (profile.preferenceId) {
ajax(`/api/posts/${profile.preferenceId}`, function (posts) {
if (posts.length) {
console.log('Preferred posts for user:', posts);
} else {
console.error('No posts for user');
}
}, function(err) {
console.error('Could not load preferences');
});
} else {
console.error('User does not have any preferences yet');
}
}, function(err) {
console.error('Could not load profile information');
});
} else {
console.log('User is not logged in.');
}
}, function(err) {
console.error('Could not load user data');
});A Promise is an object representing the eventual completion or failure of an asynchronous operation.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
const ajax = (url) => {
return new Promise((resolve, reject) => {
oldAjax(url, resolve, reject);
});
};ajax('/api/user')
.then(user => console.log(user))
.catch(err => console.error(err));ajax('/api/user')
.then(user => ajax(`/api/user/${user.id}/profile`))
.then(profile => ajax(`/api/posts/${profile.preferenceId}`)
.then(posts => console.log('Preferred posts for user:', posts);
.catch(err => console.error(err));ajax('/api/user')
.then(user => ajax(`/api/user/${user.id}/profile`))
.then(profile => ajax(`/api/posts/${profile.preferenceId}`)
.then(posts => console.log('Preferred posts for user:', posts))
.catch(err => console.error(err)); // Hvilken error?ajax('/api/user')
.then(user => ajax(`/api/user/${user.id}/profile`))
.then(profile => ajax(`/api/posts/${profile.preferenceId}`)
.then(posts => console.log('Preferred posts and user:', posts, user);
.catch(err => console.error(err));The purpose of async/await functions is to simplify the behavior of using promises synchronously and to perform some behavior on a group of Promises
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
async function fetchUser() {
const user = await ajax('/api/user');
console.log(user);
}const fetchAllTheThings = async () => {
const user = await ajax('/api/user');
const profile = await ajax(`/api/user/${user.id}/profile`);
const posts = await ajax(`/api/posts/${profile.preferenceId}`);
console.log('Preferred posts for user:', posts);
}const fetchAllTheThings = async () => {
try {
const user = await ajax('/api/user');
const profile = await ajax(`/api/user/${user.id}/profile`);
const posts = await ajax(`/api/posts/${profile.preferenceId}`);
console.log('Preferred posts for user:', posts);
} catch (e) {
console.error('Failed to load preferred posts for user');
}
}const fetchAllTheThings = async () => {
try {
const user = await ajax('/api/user');
const profile = await ajax(`/api/user/${user.id}/profile`);
const posts = await ajax(`/api/posts/${profile.preferenceId}`);
console.log('Preferred posts and user:', posts, user);
} catch (err) {
console.error('Failed to load preferred posts for user', err);
}
}const getUserData = async () => {
const [insurances, claims] = await Promise.all([
ajax('/api/insurances'),
ajax('/api/claims'),
]);
return { insurances, claims };
};+ Babel
bekk.no/jobb
By Kristofer Giltvedt Selbekk
I'm a full stack engineer that just happens to love React, CSS and front end in general