Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
This repository was archived by the owner on May 30, 2024. It is now read-only.

I want to write a function which has a return value. How to do? #39

Description

@qt911025

The function contains a synchronized process.Such as

// A getter method
var getData;
Sync(function(){//But it doe's not work.
  getData = function(path){
    var cacheDoc = cache.get(this.id);//"cache" is a "node-cache"
    if(!cacheDoc || cacheDoc[path] === undefined){
      cacheDoc = statics.findById.sync(this,this.id,cachedPathsStr);//mongoose findById
    }
    return cacheDoc[path];
  };
});

The code above will not work because of the fiber cannot be detected.

I also tried other way.

// A getter method
var getData = function(path){
  Sync(function(){
      var cacheDoc = cache.get(this.id);//"cache" is a "node-cache"
      if(!cacheDoc || cacheDoc[path] === undefined){
        cacheDoc = statics.findById.sync(this,this.id,cachedPathsStr);//mongoose findById
      }
  });
  return cacheDoc[path];
};

The execution will jump to the "return" statement unless the "return" is contained in the "Sync".
I have to put the whole function into it, and put the code which call this method.
I can't export the module if I put module.export = moduleObj into the "Sync" capsule, because of the same problem:
Error: yield() called with no fiber running

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions