.then()在内部promise解析之前自行解析

问题描述 投票:0回答:2

我有一个调用函数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。

javascript ecmascript-6 promise es6-promise
2个回答
1
投票

返回从第一个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));

检查doc of then here


0
投票

退货声明导致了一个问题。

.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));
© www.soinside.com 2019 - 2024. All rights reserved.