In my coding process, I have come across a situation where I need to fetch content and then save it using two separate functions. Each function performs a different task based on the type of content provided. These functions act as helper functions in my overall code structure.
Initially, I was using asyncfuncA().then(asyncFuncB).then(...); but I realized that I could create a new function that chains A and B together for more streamlined execution. However, I encountered a problem while trying to implement this idea. The simplified code snippet below illustrates the concept I am aiming for.
function getCont(url) {
return new Promise(function(resolve, reject) {
// Actual code makes a request and resolves with response body or rejects with an error
resolve("response body");
});
}
function saveFile(path, data) {
return new Promise(function(resolve, reject) {
// Actual code writes file and resolves true for success or rejects for error
resolve(true);
});
}
saveCont("some://url", "/some/path").then(function() {
// Execute specific actions for one type of content
});
saveCont("another://url", "/another/path").then(function() {
// Perform different actions for another type of content
});
function saveCont(url, path) {
getCont(url)
.then(function(content) {
saveFile(path, data);
})
.then(function() {
// ** Unsure about what should be included here ??
});
};