相关疑难解决方法(0)

在JavaScript中合并/展平数组数组?

我有一个JavaScript数组,如:

[["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"]]
Run Code Online (Sandbox Code Playgroud)

我将如何将单独的内部数组合并为:

["$6", "$12", "$25", ...]
Run Code Online (Sandbox Code Playgroud)

javascript arrays flatten

1001
推荐指数
36
解决办法
61万
查看次数

等待 Promise.all 中的 array.map 迭代

我有以下代码,应该为客户添加项目(如果它们尚不存在)。执行应该是并行的。

await Promise.all(
    customers.map(async (customer) => {
        return customer.items.map(async (item) => {
            return new Promise(async (resolve) => {
                const productExists = someArray.some(
                    (arrayValue) => arrayValue === item.id
                );
                if (!productExists) {
                    logger.info(
                    `customer item ${item.id} does not exist, creating...`
                    );
                    await createCustomerItem(item.id);
                    logger.info(`customer item ${item.id} created.`);

                    someArray.push(item.id);
                } else {
                    logger.info(`customer item ${item.id} already exists, skipping...`);
                }
                resolve(true);
            });
        });
    })

);

logger.info(`All items should now be present`);
Run Code Online (Sandbox Code Playgroud)

问题是createCustomerItem在以下情况下执行不会等待解决!productExists)

这是日志

customer item 32310 does not exist, creating... …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous node.js promise

6
推荐指数
2
解决办法
4万
查看次数

标签 统计

javascript ×2

arrays ×1

asynchronous ×1

flatten ×1

node.js ×1

promise ×1