Skip to content

Instantly share code, notes, and snippets.

@innerdaze
Last active June 29, 2019 05:04
Show Gist options
  • Save innerdaze/a155ff70a18049dff5d752027e5a8094 to your computer and use it in GitHub Desktop.
Save innerdaze/a155ff70a18049dff5d752027e5a8094 to your computer and use it in GitHub Desktop.
Various Promise Pattern Demonstrations
function bad() {
return new Promise(function(resolve, reject) {
if (!isValid) { reject() }
retrieveMenuFromDatabase(req.session.dbId, req.session.user.MenuGroup)
.then(function(menuResult) {
getUserFavorites(req.session.dbId, req.session.user.UserID)
.then(function(shortCutResults) {
// process both responses
resolve(calculate(shortCutResults, menuResult));
})
.catch(function(error) {
var values = req.allParams();
values.errMsg = error.message;
ResponseUtil.validateResponseFromServer(error, res, null, values);
reject(Error(error));
});
})
.catch(function(error) {
var values = req.allParams();
values.errMsg = error.message;
ResponseUtil.validateResponseFromServer(error, res, null, values);
reject(Error(error));
});
});
}
function good() {
const state = {};
if (!isValid) { return new Promise().reject(); }
return retrieveMenuFromDatabase(req.session.dbId, req.session.user.MenuGroup)
.then(function(menuResult) {
state.menuResult = menuResult;
return getUserFavorites(req.session.dbId, req.session.user.UserID);
})
.then(function(shortCutResults) {
// do loads of stuff
return calculate(shortCutResults, state.menuResult);
})
.catch(function(error) {
var values = req.allParams();
values.errMsg = error.message;
ResponseUtil.validateResponseFromServer(error, res, null, values);
throw error;
});
}
// Nesting || BAD
function nesting() {
return doAsyncThing()
.then(function (value) {
return doSecondAsyncThing(value)
.then(function(value) {
return doThirdAsyncThing(value)
.then((function (value) {
return value
})
.catch(function(error) {
handleError(error)
})
})
.catch(function(error) {
handleError(error)
})
})
.catch(function(error) {
handleError(error)
})
}
function flattened() {
return doAsyncThing()
.then(function(value) {
return doSecondAsyncThing(value)
})
.then(function(value) {
return doThirdAsyncThing(value)
})
.catch(function(error) {
handleError(error)
})
}
// notice the `throw error` line
function flattenedHandleAndPropogateError() {
return doAsyncThing()
.then(function(value) {
return doSecondAsyncThing(value)
})
.then(function(value) {
return doThirdAsyncThing(value)
})
.catch(function(error) {
handleError(error)
throw error
})
}
// if you just want the error to be handled externally, don't catch it
function flattenedPropogateError() {
return doAsyncThing()
.then(function(value) {
return doSecondAsyncThing(value)
})
.then(function(value) {
return doThirdAsyncThing(value)
})
}
// because we're just forwarding the argument from each promise to the next, we can go pointfree
function pointfreeChaining() {
return doAsyncThing()
.then(doSecondAsyncThing)
.then(doThirdAsyncThing)
.catch(handleError)
}
function renderTodos(todos) {
return `<ul>
${todos.map(function(todo) {
return `<li>todo.title</li>`
})}
</ul>`
}
function aRealWorldExample() {
return fetch('https://jsonplaceholder.typicode.com/todos')
.then(function(res) { return res.json() })
.then(renderTodos)
.catch(function(error) {
throw Error(`FetchTodoError: ${error.message}`)
})
}
// Use typed errors (probably imported from another file)
function aRealWorldExampleWithTypedErrors() {
return fetch('https://jsonplaceholder.typicode.com/todos')
.then(
function(res) { return res.json() }, // runs if fetch succeeded
function(error) { throw new FetchError(error.message)} // runs if fetch failed
)
.then(
renderTodos, // runs if res.json succeeded
function(error) { throw new JSONError(error.message)} // runs if res.json failed
)
.catch(function(error) { // runs if fetch, res.json or renderTodos failed
if (error instanceof FetchError) { /* fetch error */ }
if (error instanceof JSONError) { /* res.json error */ }
if (error instanceof Error) { /* renderTodos error */ }
})
}
@jonathanphsmith
Copy link

Great stuff, thanks Lee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment