- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对下面的代码如何工作感到相当困惑。我希望在 Promise.resolve() 返回一个已解决的 promise 之后 - 而不是一个待处理的 promise - .then() 会立即使用它/履行它,这意味着;在每个reduce回调调用中,都会问两个问题(promise链) - 但实际上它们都是链式的,并且只有在从函数chainPromises返回时,它们才会互相消耗并一起运行。
这是因为我们正在等待“调用堆栈”清空,并且每个 .then() 正在等待前一个 .then() 回调结束,因此它返回一个 promise 等?减少如何发挥作用?
有人可以帮助我更好地理解代码及其背后的概念吗?
var fs = require("fs");
var readline = require("readline");
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let questionsData = [];
promisifyQuestion('How many languages do you know? \n')
.then((numOfLanguauges) => {
return chainPromises(numOfLanguauges)
})
.then((data) => {
fs.writeFile("./langs.json", JSON.stringify(questionsData), () => {});
return promisifyQuestion('Display lang?');
})
.then(x => {
console.log(JSON.stringify(questionsData.find(el => el.name == x)));
rl.close();
});
function promisifyQuestion(msg) {
return new Promise((resolve, reject) => {
rl.question(msg, resolve);
});
}
function chainPromises(length) {
for (var i = 0; i < length; questionsData[i++] = {});
let singleValue = questionsData.reduce(outerReduce, Promise.resolve());
//Promise chain does fire only on chainPromises end
return singleValue;
}
function outerReduce(prevRes, currentElement, currentIndex) {
return prevRes
.then(() => promisifyQuestion('language name:'))
.then(x => {
questionsData[currentIndex].name = x;
return promisifyQuestion('years of speaking it:');
})
.then(x => {
return questionsData[currentIndex].years = x
});
}
最佳答案
Promise 会等待,直到 JavaScript 调用堆栈为空。下面的代码片段打印 1 2 3
,表明 .then
回调不会立即运行。从技术上讲,它们被安排在微任务队列中,与事件循环分开。
console.log('1');
Promise.resolve().then(()=>console.log('3'));
console.log('2');
您的示例更长且更复杂,但可以归结为这一点。
一些阅读 Material :
关于javascript - 一系列 promise 如何在不被消耗/履行的情况下返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49364817/
实现的在线 shopify 文档显示了这个例子: POST /admin/orders/#{id}/fulfillments.json { "fulfillment": { "tra
我有检查数组中值的函数 const arr = [10,20,30]; const check = (needle) => { return new Promi
promise.each(function(obj){ obj.doIntensiveWork() .then(function(){ console.log("I A
我正在从 Protractor 切换到 Nightwatch.js,我在 Nightwatch 处理 promise 的方式上遇到了一些困难。 举个例子,我正在尝试计算满足给定条件的元素的数量。计数函
我正在尝试使用 DialogFlow 实现 Webhook 来访问我的应用程序,但每当我发出请求时,它都会返回: POST /webhook 500 INTERNAL SERVER ERROR 来自n
使用 BlueBird promise ,我尝试将 getCredentials 变成一个将作为 getCredentials.then(function() { do this and that }
我正在使用 Dialogflow 和 Actions on Google 开发一个应用程序,该应用程序需要在 Google Assistant 中发送每日生日通知。 我正在关注this documen
对于摩卡测试,我想断言 promise 最终会拒绝。 我不想使用 chai-as-promised。我更愿意只使用 Node 的标准断言模块,并且只使用标准 ES6 Promise。 我想出的最好的就
我想用其他 promise 来履行 promise 。重点是,我真的很想在第一个 promise 实现后立即访问(仍在等待中的)第二个promise。不幸的是,我似乎只能在两个 promise 都实现
我正在使用 Node.js 开发一个使用用户证书的项目。我需要以阻塞方式同步生成它们,但我使用的库(pem)只有异步函数(回调)。我尝试了多种方法来解决这个问题,但我的尝试都没有成功。我的代码如下所示
我试图表达如下内容: it("should use the 'text' mode", () => { let usedMockMode = false; let env = new
我是一名优秀的程序员,十分优秀!