I am using only ES6 and no libraries, like Bluebird or Async. I also do not want to use async/await.
Here is the side effect code:
function getManager(name) {
return new Promise(resolve => {
resolve({
id: 123,
location: 'San Diego'
});
});
}
function getEmployees(managerId) {
return new Promise(resolve => {
resolve([
{
id: 321,
name: 'Jack'
location: 'Detroit'
},
{
id: 456,
name: 'Jill'
location: 'Cleveland'
}
]);
});
}
var manager;
getManager('Bob').then(function (result) {
manager = result;
return getEmployees(manager.id);
}).then(function (employees) {
// okay, I have both the "manager" and the "employess"
});
And here is the Pass-thru Variable:
function getManager(name) {
return new Promise(resolve => {
resolve({
id: 123,
location: 'San Diego'
});
});
}
function getEmployees(manager) {
return new Promise(resolve => {
resolve({
employees: [
{
id: 321,
name: 'Jack'
location: 'Detroit'
},
{
id: 456,
name: 'Jill'
location: 'Cleveland'
}
],
manager: manager
});
});
}
getManager('Bob').then(function (result) {
manager = result;
return getEmployees(manager);
}).then(function (results) {
// okay, I have both the "results.manager" and the "results.employess"
});
Are there other ways to handle variables in promise-chaining? Are there downsides to using side effect code? Side effect seems a little more straight forward.