yld
v1.2.0
Published
Forget your promises... adopt the yielded programming style
Downloads
28
Readme
yld
A JavaScript promises alternative, under the MIT License.
Concept :
yld (pronounced "yielded") is tool based on that keyword.
Contrary to promises, which grow to declare a lot of functions to disorganize your code, yld allows you to execute instructions written in a linear list.
Each yield pauses the process and allows to retrieve a response from another function ... could easily be compared to a promise.then().
The yld function :
var yielded = yld(closure);
yielded(/* closure arguments */);
The closure structure :
Create a step :
var closure;
closure = function () {
yield; // this makes a step
};
Retrieve a response form an other scope :
var closure
closure = function () {
var response;
response = yield;
};
The closure properties :
Object this.parent
The parent yielded scope (parent.send() & parent.yld()), if current scope is created by this.yld(), else returns undefined
String set this.error()
Kills immediately the current process
var closure;
closure = function () {
this.error = 'error message'; // throws the error message
yield; // unreached point
};
Function this.send()
Send variables to the next yield response
var closure;
closure = function () {
var response;
this.send(123);
response = yield; // response is 123
};
Function this.yld()
Your this context knows yld as internal method
var closure;
closure = function () {
var yielded, response;
yielded = this.yld(function (/* args */) {
// here, the this.parent.send() is used to send a response to the parent scope
this.parent.send(123);
});
response = yield yielded(/* args */); // response is 123
};
Requirements :
yield keyword support ES5 Object methods