简体   繁体   English

从此 nodeJS 函数访问数据

[英]access data from this nodeJS function

I want to use "data" outside of this function.我想在这个函数之外使用“数据”。 Please can some one show me how?请有人告诉我如何?

    resemble('/Users/User/Documents/dev/engineerappcopy/VGimages/'+deviceName+'.png')
             .compareTo('/Users/User/Documents/dev/engineerappcopy/VGimages/'+"nexUpdate"+'.png')
             .ignoreColors().onComplete(function(data) {
                 browser.sleep(5000)
                 console.log(data);
                 data.getDiffImage().pack().
                 pipe(fs.createWriteStream('/Users/User/Documents/dev/engineerappcopy/VGimages/'+deviceName+'VG.png'));

                         });

I am aware that this is asynchronous, however I am struggling with this.我知道这是异步的,但是我正在努力解决这个问题。

I would suggest you to use EventEmitters.我建议你使用 EventEmitters。 You can use this if at all you want to indicate if any action is finished and optionally you can pass the data as well.如果您想指示任何操作是否已完成,您可以使用它,并且您也可以选择传递数据。

Create a new javscript file 'my-emitter.js'创建一个新的 javscript 文件“my-emitter.js”

my-emitter.js (ES6 version) my-emitter.js(ES6 版本)

const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();

module.exports = myEmitter;

OR或者

my-emitter.js (Javascript version) my-emitter.js(Javascript 版本)

var EventEmitter = require('events').EventEmitter;
var util = require('util');

function MyEmitter(){
    EventEmitter.call(this);    
}

util.inherits(MyEmitter,EventEmitter);
myEmitter = new MyEmitter();

module.exports = myEmitter;

Your code snippet:您的代码片段:
(Check the comments in the code). (检查代码中的注释)。 Emit an event saying that data is available after the async operation is complete myEmitter.emit('img-op-complete',data);发出一个事件,说明异步操作完成后data可用myEmitter.emit('img-op-complete',data);

var myEmitter = require('./my-emitter.js'); //Get your emitter from the module
resemble('/Users/User/Documents/dev/engineerappcopy/VGimages/'+deviceName+'.png')
             .compareTo('/Users/User/Documents/dev/engineerappcopy/VGimages/'+"nexUpdate"+'.png')
             .ignoreColors().onComplete(function(data) {
                 browser.sleep(5000)
                 //Emit the event that data is available and pass the data
                 myEmitter.emit('img-op-complete',data);
                 console.log(data);
                 data.getDiffImage().pack().
                 pipe(fs.createWriteStream('/Users/User/Documents/dev/engineerappcopy/VGimages/'+deviceName+'VG.png'));

                         });

othermodule.js其他模块.js
Where ever you want the data (if in other module), use the below piece of code你想要数据的地方(如果在其他模块中),使用下面的代码

var myEmitter = require('./my-emitter.js'); //Get your emitter from the module
myEmitter.on('img-op-complete', function(data){
   console.log(data); //You'll get your data here after op is done
})

Fore more info on events, https://nodejs.org/dist/latest-v6.x/docs/api/events.html有关事件的更多信息, https://nodejs.org/dist/latest-v6.x/docs/api/events.html

NOTE: Promises is also nice solution, but if you use promises good design if data is needed within the same module.注意: Promises 也是不错的解决方案,但是如果在同一模块中需要数据,那么如果您使用 Promises 良好的设计。 But events present a good design pattern in node.js但是事件在 node.js 中呈现了一个很好的设计模式

I would suggest go with promises, you would be able to use this promise across module also if you export it.我建议使用承诺,如果您导出它,您也可以跨模块使用此承诺。

There are some really nice articles about how to use promises like Promise .有一些关于如何使用Promise 之类的Promise 的非常好的文章。

Coming to how to approach the above issue via promises,谈到如何通过承诺来解决上述问题,

function foo(){
    return new Promise(function(resolve,reject){
        resemble(<yourPath>)
         .compareTo(<yourPath>)
         .ignoreColors().onComplete(function(data) {
             //for best practice do handle errors 
             if(err<anyError>){
                reject(err);
             } else{
               resolve(data);
             }
        });
    })
}

Now You can use the above promise where you want to use the data variable :现在您可以在要使用数据变量的地方使用上述承诺:

foo().then(function(<data>){
    //do whatever you wish to do with the data
}).catch(function(<err>){
    //handle the error
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM