📜 ⬆️ ⬇️

News from the world of Node: promise.io, copromise, Apper

promise.io


Promise.io is a remote procedure call (RPC) module that uses promises. With it, you can create a server like this:

var server = new PromiseIO({ someFunc: function(input) { return 'I got: ' + input; } }); server.listen(3000); 

The client can call someFunc after connecting to the server:

 var client = new PromiseIO(); client.connect('http://localhost:3000').then(function(remote) { return remote.someFunc('my variable!'); }).then(function(returnVal) { return console.log(returnVal); }).catch(function(err) { return console.log(err); }); 

To realize the "promises", under the hood, q is used.
')

copromise


Copromise is a bit like co , but this library automatically returns values ​​that are not “promises”, so you can always get something using yield .

Copromise is a possible coroutine value. Where a coroutine is a generator function that uses the yield keyword to pause the launch and wait for a future value. This approach allows to obtain linear control of execution logic (following) for asynchronous code.

Dean, announced this on the node.js list, giving a few examples of using and comparing with the co library.

 function run(coroutine) { return new Promise(function(resolve, reject) { (function next(value, exception) { var result; try { result = exception ? coroutine.throw(value) : coroutine.next(value); } catch (error) { return reject(error); } if (result.done) return resolve(result.value); Promise.resolve(result.value).then(next, function(error) { next(error, true); }); })(); }); } 

Apper


Apper is a real-time framework for single-server applications. The idea is to have concrete agreements for the practical use of single-page applications, including transparent minification and packaging.

The framework described above is an over- Express wrapper, so the definition of routing routes (routes) looks like a regular Express project, although Apper has specific sections for such things as middleware or application settings.

For example:

 var app = require('apper')({ path: '.', port: 8000, host: '0.0.0.0', // Not commonly used. Just use `apper.json` for the configuration toOpenBrowser: false, staticContentName: 'public', moduleNames: { environment: 'environment' middleware: 'middleware', routes: 'routes', sockets: 'sockets' }, mountPath: '' }); app.start(); 

Where middleware.js will be:

 module.exports = function (app) { app.use(function (req, res, next) { // middleware code next(); }); }; 

If you are unfamiliar with Express , then you might like to work with the agreements that Apper uses, such an approach will at least protect you from the tendency to store everything as one large (monolithic) app.js file.

Marks


Source: https://habr.com/ru/post/236481/


All Articles