Man*_*023 2 javascript node.js promise
我正在试验 Promise.all,但是当我首先将它们推入数组时,这些函数就会运行,那么 promise.all 的意义何在?下面是一个例子:
async function foo(args) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(args)
resolve(args)
}, 500)
});
}
function init() {
var params = [10, 20, 30];
var promises = [];
for (let x of params) {
promises.push(foo(x));
}
//do something with Promise.all(promises) here
return;
}
init();
Run Code Online (Sandbox Code Playgroud)
输出:
10
20
30
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我敢肯定这是愚蠢的事情,我可能会失明。
编辑: 抱歉,我应该澄清一下,我知道如何实际调用 Promise.all,但我的印象是,在您调用之后才会运行承诺。
我想我要问的是是否有一种方法可以“查询”这些函数,以便在我运行 Promise.all() 之前它们不会立即运行?
编辑2:
我看到这被标记为重复/人们说这与 promise.all 无关,我希望尽可能在最后将函数与 promise.all 一起使用,而不是按照链接帖子的建议手动执行操作.
如果这不可能,请告诉我。
好的,首先让我们看看你的foo功能。
在这里,您有一个已解析为 Promise 的 Promise。什么是异步函数的声明确实是有效地使函数返回一个承诺。
这意味着您可以await在async函数内部使用,而无需担心函数解析。例如:
// Lets say you have a 'long running' function that returns a promise.
const timeout = ms => new Promise(
resolve => setTimeout(resolve, ms)
);
// What you can do is something like so:
async function foo(args) {
await timeout(500);
console.log(args);
return args;
}
Run Code Online (Sandbox Code Playgroud)
在“超时”功能来自@Bergi。这与您的第一个foo功能几乎完全相同。
// Lets say you 'foo' here.
const fooPromise = foo('Your args');
// The output here would be a pending Promise
// in chrome console: Promise {<pending>}
Run Code Online (Sandbox Code Playgroud)
你的foo代码所做的,实际上是相同的,除了它在实践中做这样的事情:
function foo(args) {
return new Promise(resolve1 => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(args)
resolve(args)
}, 500)
});
});
}
Run Code Online (Sandbox Code Playgroud)
这是相当多余的,但不会对程序的运行产生不利影响。
其次,让我们看看init功能。
在 JavaScript 中,您不需要使用 'Init'、'Main' 或任何等效项。你可以开始运行你的代码,这样你的init代码就可以从函数中删除并像这样执行:
var params = [10, 20, 30];
var promises = [];
for (let x of params) {
promises.push(foo(x));
}
//do something with Promise.all(promises) here
Run Code Online (Sandbox Code Playgroud)
现在再次做出承诺,您的代码并没有真正“错误”。为了在此处使用“Promise.all”,您只需按照您的评论将promises数组传递给Promise.all。
Promise.all返回一个Promise将包含您生成的promises. 然后,您可以像访问任何其他Promise.
// Use your promises
Promise.all(promises)
// Do what you need to do with the Promises
.then(output => {
// Output = [10, 20, 30]
});
Run Code Online (Sandbox Code Playgroud)
可以使用Array.prototype.map替换使用“for 循环”的代码,以快速生成承诺数组,如下所示:
var params = [10, 20, 30];
Promise.all(params.map(foo))
.then(output => {
// Output = [10, 20, 30]
});
Run Code Online (Sandbox Code Playgroud)
把它们放在一起,你会像这样使用承诺:
// A 'long running' function that returns a promise.
const timeout = ms => new Promise(
resolve => setTimeout(resolve, ms)
);
// What you can do is something like so:
async function foo(args) {
// A 'long running' function that returns a promise.
await timeout(500);
// Here you can manipulate the args like normal
// e.g.:
args = args ** 2;
console.log(args);
return args;
}
var params = [10, 20, 30];
Promise.all(params.map(foo))
.then(output => {
// output = [100, 400, 900]
console.log(output);
});
Run Code Online (Sandbox Code Playgroud)
其输出将是:
100
400
900
[100, 400, 900]
Run Code Online (Sandbox Code Playgroud)
在您的控制台中。
| 归档时间: |
|
| 查看次数: |
7435 次 |
| 最近记录: |