我有一个调用函数pngQuant的promise链
.then(response => {
console.log(response);
pngQuant.pngQuant(testObj['pngquant']);
}, err =>console.log(err))
//Then upload images
.then(response => {console.log(response)},err=>console.log(err));
function pngQuant(JSON){
return new Promise((resolve,reject)=>{
var execString = "find " + pathresolve('./src/resizer/tmp/') + " -name '*.png'"
execString += " -exec " + pathresolve('./lib/pngquant') + " --force --ext=.png";
exec(execString,(err,stdout,stderr)=>{
if(err){
console.log(err);
reject(stderr);
}
else{
console.log("resolved");
resolve("Compression Complete");
}
})
});
}
然而,链中的第二个“then”自行解析并且控制台日志“未定义”,然后立即调用pnqQuant函数中的控制台日志,这意味着在调用pngQuant resolve函数之前解析了then。
返回从第一个promise
调用pngQuant
创建的then
:
.then(response => {
console.log(response);
return pngQuant.pngQuant(testObj['pngquant']); // we returned the promise from pngQuant
}, err =>console.log(err))
// then upload images
.then(response => {
console.log(response);
}, err => console.log(err));
退货声明导致了一个问题。
.then(response => {
console.log(response);
pngQuant.pngQuant(testObj['pngquant']);
}, err =>console.log(err))
// then upload images
.then(response => {
console.log(response);
}, err => console.log(err));