【发布时间】:2018-01-03 22:00:15
【问题描述】:
我有一系列要链接在一起的 promise 函数。我不需要前一个函数的具体结果,所以我没有在 resolve(); 中添加任何内容。我只需要它们按顺序运行。
但是,包含范围中有一个变量,我想将其传递给第一个和第四个(最后一个)promise。当我将它作为参数添加到第三个承诺时,该函数立即运行。
如何将参数传递给链式承诺,而不是在那时调用函数/承诺?
这是我正在尝试做的基础知识:
const containingFunction = function() {
const varToPass = document.querySelector("#ID").value.trim();
firstPromise(varToPass)
.then(secondPromise)
.then(thirdPromise)
.then(fourthPromise(varToPass))
.catch(e =>{
console.error(e));
} );
};
第四个承诺:
const fourthPromise = function(varPassed) {
return new Promise(function(resolve, reject) {
do some stuff but only after the thirdPromise has been resolved
console.log(varPassed)
resolve();
});
};
【问题讨论】: