我有两个功能,一个用于获取内容,另一个用于保存。内容不同,我需要执行不同的任务。我事先知道什么类型的内容,这两个只是助手功能。
我正在使用asyncfuncA()。然后(asyncFuncB).then(...);但我认为我可以使用链接A和B的函数并像asyncfuncAB.then(...)一样使用它,但我遇到了问题。以下简化代码解释了我想做的事情。
function getCont(url) {
return new Promise(function (resolve ,reject) {
// The real code makes request and resolves to response body or rejects with error
resolve("response body");
});
}
function saveFile (path,data) {
return new Promise(function (resolve ,reject) {
// The real code writes file and resolves to true for success or rejects for error
resolve(true);
});
}
saveCont("some://url","/some/path").then ( function() {
// Do some thing for one type of content
});
saveCont("another://url","/another/path").then ( function() {
// Do another thing for another type of content
});
function saveCont(url,path) {
getCont(url)
.then(function(content) {
saveFile(path,data) })
.then( function() {
// ** What to put here ??
});
};
答案 0 :(得分:0)
你最后不需要放任何东西,只需返回Promise
对象:
function saveCont(url,path) {
return getCont(url) //CHANGED
.then(function(content) {
return saveFile(path,data); // if you do not put a return here, saveFile would still work, but the next step in chain would not wait till SaveFile finishes processing.
});
};