为什么我的承诺(运行 并行和 运行 串行)同时完成
Why are my promises (running in parallel and running in serial) completing at the same time
我正在尝试查看 运行 承诺的并行顺序和顺序顺序之间的处理时间差异。但是在下面的代码中,我同时获得了两个函数的输出。理想情况下,并行函数结果应该更快出现。我这里有什么问题吗?
const timeout = 10000
const function1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("hello1")
}, timeout);
})
const function2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("hello2")
}, timeout);
})
const parallel = async () => {
const result1 = function1
const result2 = function2
const result = await Promise.all([result1, result2])
console.log(result)
}
const serial = async () => {
const result1 = await function1
const result2 = await function2
console.log("result", result1 + " : "+result2)
}
parallel()
serial()
这里有两个关键点:
承诺根本不是“运行”。 promise 是一种观察 正在运行 完成的事情的方式,它们不会运行 任何事情。 你不是唯一对此感到困惑的人。 :-) 这是一个非常普遍的误解。
您的 function1
和 function2
不是函数,它们是包含承诺的常量。一旦您调用 new Promise
,您在承诺执行器 运行 中的代码就会启动您的计时器。 Promise
构造函数同步调用 promise 执行器,以启动 promise 将报告完成的任何异步操作。
这就是您看到当前结果的原因:您同时启动所有计时器,因此它们会同时触发。无论您是观察 并行还是串行完成这些都没有关系。
如果您想看到差异,请等待开始您的操作:
const timeout = 1000; // <== Changed to 1s
const function1 = () => new Promise((resolve, reject) => {
// Note the −−−−−−^^^^^−−− change, I've made `function1` actually a function
setTimeout(() => {
resolve("hello1");
}, timeout);
});
// Note the change, I've made `function2` actually a function
const function2 = () => new Promise((resolve, reject) => {
// Note the −−−−−−^^^^^−−− change, I've made `function2` actually a function
setTimeout(() => {
resolve("hello2");
}, timeout);
});
const parallel = async () => {
const result1 = function1(); // <== Calling the function starts the timer
const result2 = function2(); // <== Calling the function starts the timer
const result = await Promise.all([result1, result2]);
console.log(result);
};
const serial = async () => {
const result1 = await function1(); // <== Calling the function starts the timer
const result2 = await function2(); // <== Calling the function starts the timer
console.log("result", result1 + " : " + result2);
};
parallel();
serial();
我正在尝试查看 运行 承诺的并行顺序和顺序顺序之间的处理时间差异。但是在下面的代码中,我同时获得了两个函数的输出。理想情况下,并行函数结果应该更快出现。我这里有什么问题吗?
const timeout = 10000
const function1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("hello1")
}, timeout);
})
const function2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("hello2")
}, timeout);
})
const parallel = async () => {
const result1 = function1
const result2 = function2
const result = await Promise.all([result1, result2])
console.log(result)
}
const serial = async () => {
const result1 = await function1
const result2 = await function2
console.log("result", result1 + " : "+result2)
}
parallel()
serial()
这里有两个关键点:
承诺根本不是“运行”。 promise 是一种观察 正在运行 完成的事情的方式,它们不会运行 任何事情。 你不是唯一对此感到困惑的人。 :-) 这是一个非常普遍的误解。
您的
function1
和function2
不是函数,它们是包含承诺的常量。一旦您调用new Promise
,您在承诺执行器 运行 中的代码就会启动您的计时器。Promise
构造函数同步调用 promise 执行器,以启动 promise 将报告完成的任何异步操作。
这就是您看到当前结果的原因:您同时启动所有计时器,因此它们会同时触发。无论您是观察 并行还是串行完成这些都没有关系。
如果您想看到差异,请等待开始您的操作:
const timeout = 1000; // <== Changed to 1s
const function1 = () => new Promise((resolve, reject) => {
// Note the −−−−−−^^^^^−−− change, I've made `function1` actually a function
setTimeout(() => {
resolve("hello1");
}, timeout);
});
// Note the change, I've made `function2` actually a function
const function2 = () => new Promise((resolve, reject) => {
// Note the −−−−−−^^^^^−−− change, I've made `function2` actually a function
setTimeout(() => {
resolve("hello2");
}, timeout);
});
const parallel = async () => {
const result1 = function1(); // <== Calling the function starts the timer
const result2 = function2(); // <== Calling the function starts the timer
const result = await Promise.all([result1, result2]);
console.log(result);
};
const serial = async () => {
const result1 = await function1(); // <== Calling the function starts the timer
const result2 = await function2(); // <== Calling the function starts the timer
console.log("result", result1 + " : " + result2);
};
parallel();
serial();