小编Ton*_*ony的帖子

提供Promise作为模块的导出是Node.js中异步初始化的有效模式吗?

我需要编写一些加载数据的模块,然后提供该数据的接口.我想异步加载数据.我的应用程序已经使用了promises.是否要求模块有效的模式/习语是否提供了承诺?

示例模块:

var DB = require('promise-based-db-module');

module.exports =
  DB.fetch('foo')
  .then(function(foo){
    return {
        getId: function(){return foo.id;},
        getName: function(){return foo.name;}
    };
  });
Run Code Online (Sandbox Code Playgroud)

用法示例:

require('./myPromiseModule')
.then(function(dataInterface){
  // Use the data
});
Run Code Online (Sandbox Code Playgroud)

更新:

我现在已经使用了一段时间了,效果很好.我已经学到的一件事,它在接受的答案中暗示的是,缓存承诺本身以及每当您想要访问数据时都是好的then.第一次访问数据时,代码将一直等到promise被解决.后续使用then将立即返回数据.例如

var cachedPromise = require('./myPromiseModule');
cachedPromise.then(function(dataInterface){
  // Use the data
});
...
cachedPromise.then(function(dataInterface){
  // Use the data again somewhere else.
});
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise node-modules

27
推荐指数
1
解决办法
9754
查看次数

在Visual Studio 2010中,如何搜索不在单行注释中的文本?

在Visual Studio 2010中,如何搜索不在单行注释中的文本?EG如何找到"bas":

foo bar bas
Run Code Online (Sandbox Code Playgroud)

但不是

foo bar // bas
Run Code Online (Sandbox Code Playgroud)

请注意,它应该找到该行:

foo / bar / bas
Run Code Online (Sandbox Code Playgroud)

(编辑)它不应该找到该行:

foo // bar bas
Run Code Online (Sandbox Code Playgroud)

regex search visual-studio-2010 visual-studio regex-negation

8
推荐指数
2
解决办法
3308
查看次数