簡體   English   中英

如何稱呼這個諾言鏈正確

[英]How to call this promise chain right

我將以下代碼與blueBird lib一起使用,但是在控制台中出現錯誤:

Uncaught TypeError:無法讀取未定義的屬性“ then”

而且我在那時 ([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與以下代碼

 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"); }) 

您對Promise是什么有誤解。 承諾是價值的代表 ,而非行動的代表 傳遞給Promise構造函數的代碼將立即執行,因此您的代碼將始終一次運行,而不會一次運行。 (您不能像運行“數字”或布爾值一樣“運行” Promise。但是,您可以運行函數)

您要做的是具有step1step2 函數 ,它們返回Promise

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');

你不能像這樣鎖鏈

stepOne.then(function () {

}).stepTwo.then(function () {

如你想訪問stepTwo從返回結果的屬性then ,它不具有stepTwo財產。

您必須這樣做,將變量(和promise)分開

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");
});

如果你需要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");
});

但是,我必須添加一下,當拒絕第一個promise(在內部catch處理它之后)然后解析它沒有任何意義時, stepTwo會調用console.log (說“成功”)和stepTwo 我想這只是代碼的抽象,並且您的重試機制是不同的,因此無論如何它都可以按需工作...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM