一个承诺链如何return而不被consumed/fulfilled?
How does a chain of promises return without being consumed/fulfilled?
我对下面代码的工作原理感到很困惑。
我希望在 Promise.resolve() returns 一个已解决的承诺之后 - 而不是一个未决的承诺 - .then() 会立即消耗 it/fulfill 它,意思是;
在每个 reduce 回调调用中,将询问两个问题(承诺链)——但事实上它们都是链式的,并且仅在来自函数 chainPromises 的 return 上,它们是否相互消耗并 运行 在一起.
这是因为我们正在等待 'call stack' 清空并且每个 .then() 都在等待前一个 .then() 回调结束所以它 return 是一个承诺,等等.?
reduce 如何发挥作用?
有人可以帮助我更好地理解代码及其背后的概念吗?
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
});
}
Promises 等到 JavaScript 调用堆栈为空。下面的代码片段打印 1 2 3
,表明 .then
回调不会立即 运行。从技术上讲,它们被安排在 微任务队列 中,与 事件循环 .
分开
console.log('1');
Promise.resolve().then(()=>console.log('3'));
console.log('2');
你的例子更长更复杂,但归结起来就是这样。
一些阅读material:
Promise.resolve()
returns a fulfilled promise - not a pending one - .then()
whould immediately consume it
是的,没错。
in each reduce callback call, two questions will be asked (the chain of promises)
没有。只有链中的 first then
回调被 immediately 调用。其他 then
调用不是针对初始 Promise.resolve()
值,而是针对之前 returned 的值。这些 不会 立即实现,它们解决并等待 then
回调 return.
的承诺
我对下面代码的工作原理感到很困惑。 我希望在 Promise.resolve() returns 一个已解决的承诺之后 - 而不是一个未决的承诺 - .then() 会立即消耗 it/fulfill 它,意思是; 在每个 reduce 回调调用中,将询问两个问题(承诺链)——但事实上它们都是链式的,并且仅在来自函数 chainPromises 的 return 上,它们是否相互消耗并 运行 在一起.
这是因为我们正在等待 'call stack' 清空并且每个 .then() 都在等待前一个 .then() 回调结束所以它 return 是一个承诺,等等.? reduce 如何发挥作用?
有人可以帮助我更好地理解代码及其背后的概念吗?
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
});
}
Promises 等到 JavaScript 调用堆栈为空。下面的代码片段打印 1 2 3
,表明 .then
回调不会立即 运行。从技术上讲,它们被安排在 微任务队列 中,与 事件循环 .
console.log('1');
Promise.resolve().then(()=>console.log('3'));
console.log('2');
你的例子更长更复杂,但归结起来就是这样。
一些阅读material:
Promise.resolve()
returns a fulfilled promise - not a pending one -.then()
whould immediately consume it
是的,没错。
in each reduce callback call, two questions will be asked (the chain of promises)
没有。只有链中的 first then
回调被 immediately 调用。其他 then
调用不是针对初始 Promise.resolve()
值,而是针对之前 returned 的值。这些 不会 立即实现,它们解决并等待 then
回调 return.