The question of generator writing asynchrony?

  node.js, question

Question: Enter the parameters of path and suffix respectively at runtime to print out the qualified files.
I don’t know how to use generator, but I always report mistakes. Please give me some advice.

'use strict'
 var fs = require("fs");
 
 var arr = [];
 function* test() {
 fs.readdir(process.argv[2], (err, list) => {
 //console.log(list);
 list.forEach((val, index) => {
 let temp = val.split(".");
 if (temp[1] === process.argv[3]) {
 arr.push(val);
 //console.log(arr);
 bracket
 });
 yield arr;
 });
 bracket
 var generator = test();
 var arr1 = generator.next().value;
 arr1.forEach((val,index)=>{
 console.log(val);
 });

The yield statement cannot be used in ordinary functions, otherwise an error will be reported.

The callback function of fs.readdir inside is a common function

'use strict'
 var fs = require("fs");
 
 
 
 function run(gen){
 var gen_obj=gen(resume);
 function resume(){
 //gen_obj.next(arguments);
 var list=arguments[1];
 var arr=[];
 list.forEach((val,index)=>{
 let tmp = val.split(".");
 if(tmp[1] === process.argv[3]){
 arr.push(val);
 bracket
 });
 console.log(arr);
 gen_obj.next(arr);
 bracket
 gen_obj.next();
 bracket
 
 run(function* gen(resume) {
 var ret,err,data;
 ret=yield fs.readdir(process.argv[2],resume);
 
 
 console.log(ret);
 });