如何正确地称这个承诺链

时间:2015-12-28 12:26:05

标签: javascript promise

我在blueBird lib中使用以下代码但在控制台中出现错误:

  

未捕获的TypeError:无法读取属性'然后'未定义的

我没有看到那么([SUCESS])中的console.log为什么?

我有两个档案 1.index.html,代码如下

<html>
<script src='https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.1.1/bluebird.js'></script>
  1. 和script.js,代码如下
  2. &#13;
    &#13;
    var stepOne = new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log("Step 1 -->Successfully Resolving");
            resolve();
        }, 5000);
        setTimeout(function () {
            console.log("Step 1 -->First timeout, rejecting the promise");
            reject();
        }, 2000);
    }).catch(function () {
            console.log("Step 1 -->Timed out 1 ... retrying");
        });
    
    
    var stepTwo = new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log("Step 2 -->Successfully Resolving Step two");
            resolve();
        }, 5000);
        setTimeout(function () {
            console.log("Step 2 -->First timeout, rejecting the promise");
            reject();
        }, 2000);
    }).catch(function () {
            console.log("Step 2 -->timed out 2 ... retrying");
        });
    
    
    stepOne.then(function () {
        console.log("[SUCCESS] Step 1");
    }).stepTwo.then(function () {
            console.log("[Success] Step 2");
        })
    &#13;
    &#13;
    &#13;

3 个答案:

答案 0 :(得分:5)

你对Promise是什么有点误解。 Promise是的代理,而不是操作的代理。传递给Promise构造函数的代码立即执行 ,因此您的代码将始终一次性运行,而不是一个接一个地运行。 (你不能“运行”一个Promise,就像你不能“运行”一个数字或一个布尔值。但是,你可以运行一个函数)

您要做的是拥有step1step2 功能返回承诺

const step1 = () => new Promise(...); // Step 1 code in this promise
const step2 = () => new Promise(...); // Step 2 code in this promise

// By this point, nothing runs, we only defined functions.

step1() // Run step one
  .then(() => console.log('[SUCCESS] Step 1');
  .then(step2); // Run step two
  .then(() => console.log('[SUCCESS] Step 2');

答案 1 :(得分:1)

你不能像这样链接

stepOne.then(function () {

}).stepTwo.then(function () {

当您尝试访问stepTwo的返回结果的then属性时,该属性没有stepTwo属性。

你必须这样做,保持变量(和承诺)分开

stepOne.then(function () {
    console.log("[SUCCESS] Step 1");
});

stepTwo.then(function () {
        console.log("[Success] Step 2");
});

或者如果您想等待所有承诺完成,您可以使用Promise.all

Promise.all([stepOne, stepTwo]).then(function() {
    console.log("both");
});

答案 2 :(得分:-2)

如果您需要在stepOne之前发生stepTwo,请执行以下操作:

var stepOne = function() {
    var stepOnePromise = new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log("Step 1 -->Successfully Resolving");
            resolve();
        }, 5000);
        setTimeout(function () {
            console.log("Step 1 -->First timeout, rejecting the promise");
            reject();
        }, 2000);
    }).catch(function () {
        console.log("Step 1 -->Timed out 1 ... retrying");
    });
    return stepOnePromise;
}

var stepTwo = function() {
    var stepTwoPromise = new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log("Step 2 -->Successfully Resolving Step two");
            resolve();
        }, 5000);
        setTimeout(function () {
            console.log("Step 2 -->First timeout, rejecting the promise");
            reject();
        }, 2000);
    }).catch(function () {
        console.log("Step 2 -->timed out 2 ... retrying");
    });
    return stepTwoPromise;
}

stepOne().then(function () {
    console.log("[SUCCESS] Step 1");
})
.then(stepTwo)
.then(function () {
    console.log("[Success] Step 2");
});

我必须补充一点,现在当拒绝第一个承诺(在内部{{1}之后)时,console.log(表示“成功”)以及stepTwo将被调用。处理它)然后解决它没有任何意义。我想这只是代码的抽象,你的重审机制是不同的,所以无论如何它可能会按照需要工作......