async在await循环中使用是否有任何问题?我正在尝试循环遍历文件数组和forEach每个文件的内容.
import fs from 'fs-promise'
async function printFiles () {
const files = await getFilePaths() // Assume this works fine
files.forEach(async (file) => {
const contents = await fs.readFile(file, 'utf8')
console.log(contents)
})
}
printFiles()
Run Code Online (Sandbox Code Playgroud)
这段代码确实有效,但这可能会出错吗?我有人告诉我你不应该使用await这样的高阶函数,所以我只是想问一下这是否有任何问题.
之间有什么区别:
const [result1, result2] = await Promise.all([task1(), task2()]);
Run Code Online (Sandbox Code Playgroud)
和
const t1 = task1();
const t2 = task2();
const result1 = await t1;
const result2 = await t2;
Run Code Online (Sandbox Code Playgroud)
和
const [t1, t2] = [task1(), task2()];
const [result1, result2] = [await t1, await t2];
Run Code Online (Sandbox Code Playgroud) 如何更改以下代码,以便触发异步操作并同时运行?
const value1 = await getValue1Async();
const value2 = await getValue2Async();
// use both values
Run Code Online (Sandbox Code Playgroud)
我需要做这样的事情吗?
const p1 = getValue1Async();
const p2 = getValue2Async();
const value1 = await p1;
const value2 = await p2;
// use both values
Run Code Online (Sandbox Code Playgroud) MDN说 for await...of有两个用例:
该
for await...of语句创建一个循环迭代异步可迭代对象以及同步可迭代对象,...
我以前知道前者:使用Symbol.asyncIterator. 但我现在对后者感兴趣:同步迭代。
以下代码迭代一个同步可迭代对象 - 一个 promise 数组。它似乎阻碍了每个承诺的实现。
async function asyncFunction() {
try {
const happy = new Promise((resolve)=>setTimeout(()=>resolve('happy'), 1000))
const sad = new Promise((_,reject)=>setTimeout(()=>reject('sad')))
const promises = [happy, sad]
for await(const item of promises) {
console.log(item)
}
} catch (err) {
console.log(`an error occurred:`, err)
}
}
asyncFunction() // "happy, an error occurred: sad" (printed in quick succession, after about 5 seconds)Run Code Online (Sandbox Code Playgroud)
根据下面显示的逻辑,这种行为似乎类似于依次等待每个承诺。这个说法正确吗?
async function asyncFunction() {
try { …Run Code Online (Sandbox Code Playgroud)我正在尝试使用用户设置查询后期集合,但设置是一个包含 10 多个元素的数组,并且没有返回任何内容。我知道文档确实提到了 10 个元素的限制,有人知道解决方法吗?
firebaseApp.collection('posts')
.where("newTag", "in", mySettings)
.get()
let array = [];
posts.forEach((post) => {
array.push(post.data());
});
dispatch({ type: ActionTypes.GET_POSTS, payload: array });
Run Code Online (Sandbox Code Playgroud) 这是一个定时器函数
const timer = (time) => {
return new Promise((resolve, reject) => {
console.log(`${time} timer start`);
setTimeout(() => {
console.log(`${time} timer end`);
resolve();
}, time);
});
};
Run Code Online (Sandbox Code Playgroud)
我将用“for wait of”和“for of”来调用这个计时器函数。
首先,“等待”
async function runForAwaitOf() {
const times = [300, 100, 700, 500];
for await (let time of times) {
await timer(time);
}
console.log('All the timers end');
}
runForAwaitOf()
Run Code Online (Sandbox Code Playgroud)
二、“为之”
async function runForOf() {
const times = [300, 100, 700, 500];
for (let time of times) {
await timer(time);
} …Run Code Online (Sandbox Code Playgroud) (英语不是我的母语,所以如果您发现任何英语,请提前原谅我的英语)
此时,我对 Promises 和 Async wait 函数非常满意,我也知道如何使用 Promise.all (它只是等待内部的所有承诺首先解析,然后从所有承诺中提取值,然后返回一个数组将 .then 函数与这些值一起使用。)
现在我想知道 for wait 在幕后是如何工作的。
我刚刚写了这段代码:
async function f() {
let p1 = new Promise(function (r) {
setTimeout(r, 3000, 1)
})
let p2 = new Promise(function (r) {
setTimeout(r, 2000, 2)
})
let p3 = new Promise(function (r) {
setTimeout(r, 1000, 3)
})
let arrayOfPromises = [p1, p2, p3];
for await (let p of arrayOfPromises) {
let data = await p;
console.log(data)
}
}
f();Run Code Online (Sandbox Code Playgroud)
现在我的问题是,当它命中第一次迭代时会发生什么,它将命中await关键字,并且await立即返回一个待处理的承诺,那么下面的代码在技术上是否在每次迭代中评估为待处理的承诺?
{
let data …Run Code Online (Sandbox Code Playgroud) javascript ×7
async-await ×5
promise ×3
asynchronous ×1
es2017 ×1
firebase ×1
for-loop ×1
node.js ×1