Node.js 如何实现npm包脚本的连续循环

Node.js 如何实现npm包脚本的连续循环,node.js,asynchronous,npm,Node.js,Asynchronous,Npm,我的代码是 我的index.js脚本如下: import personfaces from './dist/index'; const dnte = new personfaces(); dnte.getImage({ width: 256, height: 256, type: 'file' }) .then(res => { console.log('result->', res); }) .catch(err => { console.log('error->'

我的代码是

我的index.js脚本如下:

import personfaces from './dist/index';

const dnte = new personfaces();

dnte.getImage({ width: 256, height: 256, type: 'file' })
.then(res => { console.log('result->', res); })
.catch(err => { console.log('error->', err); });
我使用终端命令“npmstart”运行代码,但我想让它成为一个连续的重复循环,比如说100次运行。 每次我在终端上运行代码时,它都会下载一个图像,我希望它是一个连续运行100次的过程,以便生成100个图像


是否有办法运行终端命令100次运行或将100次运行添加到我的主代码中。

是的,您可以,但就此而言,您不需要它。你必须运行你的函数一百次。否则,您可以创建一个bash文件,并根据您的操作系统运行它,然后运行它‍‍<代码>npm启动每次代码。操作系统中的作业或计划

但最简单的方法是使用递归函数

import personfaces from './dist/index';

const dnte = new personfaces();

var count = 100
recursiveFun(0);
function recursiveFun(index) {
  if (index === count)
    return;
  dnte.getImage({ width: 256, height: 256, type: 'file' })
    .then(res => {
      console.log('result->', res);
      recursiveFun(index + 1)
    })
    .catch(err => {
      console.log('error->', err);
      recursiveFun(index)
    });
}
第二种方法是使用节点模块:

npm上的模块实现了数组迭代方法,因此它们可以以非常简单的方式与async/await一起使用

循环遍历数组以对每个元素执行异步操作。 有时必须执行,但必须等待上一个操作完成后才能继续下一个操作