在类的Node JS中使用生成器

时间:2021-05-02 23:31:01

Alright, I need some help with generators in Node.

好的,我需要一些帮助,在节点的生成器。

I really want to write code that looks like this:

我真的想写这样的代码:

require('./Class.js');
fs = require('fs');

var Directory = Class.extend({

    construct: function(path){
        this.path = path;
    },

    list: function*() {
        var files = [];

        yield fs.readdir(this.path, function(error, directoryFiles) {
            files = directoryFiles;
        });

        return files;
    },

});

var directory = new Directory('C:\\');
var list = directory.list();
console.log(list); // An array of files

Notes:

注:

Is something like this possible?

这可能吗?

2 个解决方案

#1


2  

You could use promises to do this so that you have proper async handling as well as built-in support for any errors.

您可以使用承诺来实现这一点,这样您就有了合适的异步处理,以及对任何错误的内置支持。

list: function() {
  var deferred = q.defer();
  fs.readdir(this.path, deferred.makeNodeResolver());
  return deferred.promise;
}


directory.list()
  .then(function(files) { console.log(files); })
  .fail(function(err) { res.send(500,err); });

#2


2  

You can use a helper lib like Wait.for-ES6 (I'm the author)

你可以像等待一样使用助手库。for-ES6作者(我)

Using wait.for your "list" function will be a standard async function:

使用等。对于您的“列表”函数将是一个标准的异步函数:

 ...
list: function(callback) {
        fs.readdir(this.path, callback);
    },
 ...

but you can call it sequentially IF you're inside a generator:

但是如果你在生成器中,你可以按顺序调用它:

function* sequentialTask(){
   var directory = new Directory('C:\\');
   var list = yield wait.forMethod(directory,'list');
   console.log(list); // An array of files
}

wait.launchFiber(sequentialTask);

Pros: You can call sequentially any standard async node.js function

优点:可以按顺序调用任何标准的异步节点。js函数

Cons: You can only do it inside a generator function*

缺点:只能在生成器函数*中执行

Another example: You can even do it without the 'list' function since fs.readdir IS a standard async node.js function

另一个例子:如果没有“list”函数,你甚至可以做到。readdir是标准的异步节点。js函数

var wait=require('wait.for-es6'), fs=require('fs');

function* sequentialTask(){
   var list = yield wait.for(fs.readdir,'C:\\');
   console.log(list); // An array of files
}

wait.launchFiber(sequentialTask);

#1


2  

You could use promises to do this so that you have proper async handling as well as built-in support for any errors.

您可以使用承诺来实现这一点,这样您就有了合适的异步处理,以及对任何错误的内置支持。

list: function() {
  var deferred = q.defer();
  fs.readdir(this.path, deferred.makeNodeResolver());
  return deferred.promise;
}


directory.list()
  .then(function(files) { console.log(files); })
  .fail(function(err) { res.send(500,err); });

#2


2  

You can use a helper lib like Wait.for-ES6 (I'm the author)

你可以像等待一样使用助手库。for-ES6作者(我)

Using wait.for your "list" function will be a standard async function:

使用等。对于您的“列表”函数将是一个标准的异步函数:

 ...
list: function(callback) {
        fs.readdir(this.path, callback);
    },
 ...

but you can call it sequentially IF you're inside a generator:

但是如果你在生成器中,你可以按顺序调用它:

function* sequentialTask(){
   var directory = new Directory('C:\\');
   var list = yield wait.forMethod(directory,'list');
   console.log(list); // An array of files
}

wait.launchFiber(sequentialTask);

Pros: You can call sequentially any standard async node.js function

优点:可以按顺序调用任何标准的异步节点。js函数

Cons: You can only do it inside a generator function*

缺点:只能在生成器函数*中执行

Another example: You can even do it without the 'list' function since fs.readdir IS a standard async node.js function

另一个例子:如果没有“list”函数,你甚至可以做到。readdir是标准的异步节点。js函数

var wait=require('wait.for-es6'), fs=require('fs');

function* sequentialTask(){
   var list = yield wait.for(fs.readdir,'C:\\');
   console.log(list); // An array of files
}

wait.launchFiber(sequentialTask);