![](http://habrastorage.org/getpro/habr/avatars/784/f03/852/784f038524dc1c2c36f2a292799d7e79.jpg)
News from the world of Node: promise.io, copromise, Apper
- Transfer
promise.io
Promise.io is a module for remote procedure call (RPC) that uses promises. Using it, you can create such a server:
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 implement 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 always have the opportunity to get something using it
yield
.Copromise is a possible coroutine value . Where coroutine is a generator function that uses a keyword yield
to pause startup and wait for a future value. This approach allows to obtain linear control of the execution (following) logic for asynchronous code.
Dean, announced this on the node.js list with several examples of use and comparison 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-station applications. The idea is to have reinforced concrete agreements for the practical use of single-page applications, including transparent minification and bundling.
The above framework is an over- Express wrapper, so the definition of routes looks like in a regular Express project , although Apper has specific sections for things like middleware or application settings.
For instance:
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 conventions that Apper uses, this approach will at least save you from the tendency to store everything in one large (monolithic) file
app.js
.Tagging
- Promise.io: (GitHub: krillr / promise.io , Lick: Apache 2.0, npm: promise.io ) by Aaron Krill.
- Copromise: (GitHub: deanlandolt / copromise , License: MIT, npm: copromise ) by Dean Landolt.
- Apper: (GitHub: asyncanup / apper , License: MIT, npm: apper ) by Anup Bishno.
- In my opinion, the basic idea that Apper is a kind of guide and over-framework in the world of "structured ++" Express applications is not quite expressed here .