[英]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>
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。但是,您可以运行函数)
您要做的是具有step1
和step2
函数 ,它们返回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.