This Presentation, on YouTube:
https://youtu.be/xkc2ejOpStw
Helpful links:
AJ ONeal
@_beyondcode
twitch.tv/coolaj86
Dangerous Wrong Thinker
Equal Opportunity Offender
Technophobic Technologist Extraordinairé
Utah Node.js
Utah Rust
twitch.tv/coolaj86
What’s a Promise?
let promise = {};
let promise = {};
A promise is an object
let promise = { _callbacks: [], };
let promise = { _callbacks: [], };
with an array for success handlers
(callback functions)
let promise = { _callbacks: [], then: function (fn) { promise._callbacks.push(fn); }, };
let promise = { _callbacks: [], then: function (fn) { promise._callbacks.push(fn); }, };
and a then
function.
just a fancy
arr.push()
let promise = { _callbacks: [], _errbacks: [], then: function (fn) { promise._callbacks.push(fn); }, };
It also has an array for error handlers (errbacks)
let promise = { _callbacks: [], _errbacks: [], then: function (fn) { promise._callbacks.push(fn); }, catch: function (fn) { promise._errbacks.push(fn); }, };
let promise = { _callbacks: [], _errbacks: [], then: function (fn) { promise._callbacks.push(fn); }, catch: function (fn) { promise._errbacks.push(fn); }, };
and a catch()
another fancy arr.push()
function
let promise = { _callbacks: [], _errbacks: [], // ... resolve: function (result) { promise._callbacks.forEach(function (fn) { fn(result); }); }, };
let promise = { _callbacks: [], _errbacks: [], // ... resolve: function (result) { promise._callbacks.forEach(function (fn) { fn(result); }); }, };
The resolve
function
which calls the success
handlers
let promise = { _callbacks: [], _errbacks: [], // ... resolve: function (result) { promise._callbacks.forEach(function (fn) { fn(result); }); }, reject: function (err) { promise._errbacks.forEach(function (fn) { fn(err); }); }, };
let promise = { _callbacks: [], _errbacks: [], // ... resolve: function (result) { promise._callbacks.forEach(function (fn) { fn(result); }); }, reject: function (err) { promise._errbacks.forEach(function (fn) { fn(err); }); }, };
And the reject
function
which calls the
success handlers
When you want to handle an event ONE TIME:
document.addEventListener("click", handleClick);
function handleClick() {
// ...
document.removeEventListener("click", handleClick);
}
When you want to handle an event in MANY PLACES:
function doFoo() {
// ...
document.addEventListener("click", handleClick);
}
function doBar() {
// ...
document.addEventListener("click", handleClick);
}
When you want to handle an event LATE:
// click happens... NOW
function doFoo() {
// ...
document.addEventListener("click", handleClick);
}
// doFoo happens LATE
doFoo();
Available in JavaScript since 1997
promise.then(function (result) {
// do stuff
});
promise.catch(function (err) {
if ("NOT_FOUND" === err.code) {
return Foo.create();
}
throw err;
});
promise
.catch(function (err) {
if ("NOT_FOUND" === err.code) {
return Foo.create();
}
throw err;
})
.then(function (result) {
// do stuff
});
🔙 that thing back there
Or, since 2015:
async function getUsers() {
return [];
}
Or, since 2015:
async function getUsers() {
return [];
}
getUsers().then(function (users) {
// ...
});
Or, since 2015:
async function getUsers() {
return [];
}
getUsers().then(function (users) {
// ...
});
let users = await getUsers();
Always return await
.
async function getUsers() {
return await DB.Users.all();
}
Always await .catch()
.
let user = Users.getOrCreate(email).catch(function (err) {
if ("NOT_FOUND" === err.code) {
return Foo.create();
}
throw err;
});