JS 2017
Promises
Best or Worse
a lot of API based on Promises
fetch
Response and Request
Headers and Body
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
What is the problem with fetch API?
Cancel
How do you cancel?
Tasks
toTask(getUser()
.then(populateGroups)
.then(checkDriver)
.then(getTrips)
.then(getAddresses)
).andThen(render)
Cancellation tokens
var { token, cancel } = CancelToken.source();
getUsers({}, token).then(render)
cancel();
async function getUsers (data, token) {
var users = await getUsersFromDB(data, token);
token.throwIfRequested();
let fullUsers = await populateUsers(users, token);
token.throwIfRequested();
return fullUsers;
}
async function getUsers (data, token) {
await.cancelToken = token;
var users = await getUsersFromDB(data, token);
let fullUsers = await populateUsers(users, token);
return fullUsers;
}
Generators
function* getNumbers() {
yield 42;
yield 24;
return 1;
}
Consumer vs Producer
What is a problem with generators
function* getLines () {
var reader = new FileReader("Some.txt");
while (!reader.oef) {
let line = reader.readLine();
yield JSON.parse(line);
}
reader.close();
return "";
}
They are sync
We can't use them with WebSockets
async function* getLines () {
var reader = new AsyncFileReader("Some.txt");
while (!reader.oef) {
let line = await reader.readLine();
yield JSON.parse(line);
}
await reader.close();
}
var iterator = getLines();
iterator.next().then(() => {});
iterator.next().then(() => {});
iterator.next().then(() => {});
iterator.next().then(() => {});
for-of
for await(let line of getLine()) {
// here is line
}
WebSockets and Node Streams
Observer
interface Observer<T> {
next (data: T): void,
error (e: Error): void,
complete (data: T): void
}
interface Observable<T> {
subscribe (obs: Observer<T>, cancelToken): {
unsubscribe (): void
}
}
What is also new?
DOM 4
forEach, entities, keys
passive, once events
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
append, prepend, remove, before, after, closest, contains, matches
JS 2017
By Vladimir
JS 2017
- 180