Skip to content

Instantly share code, notes, and snippets.

@rctay
Last active December 19, 2015 01:09
Show Gist options
  • Save rctay/5873648 to your computer and use it in GitHub Desktop.
Save rctay/5873648 to your computer and use it in GitHub Desktop.
/**
* Basically like doWithM, just that your callback need not call done() at the
* end.
*
* ex.
* var linkFuture = doWith(element("a.myLink").attr("href"), function(href) {
* console.log("got link", href);
* });
*/
angular.scenario.dsl('doWith', function() {
return function(future, fn, desc) {
var self = this;
return this.dsl.doWithM(future, function(value, done) {
try {
fn.call(this, value);
done(null, value);
} catch (e) {
done(e.message);
}
}, desc);
};
});
/**
* Usage:
* doWith(dsl(), callback)
* doWith(dsl(), callback, description)
* where
* dsl is a value-yielding dsl API function eg. element(sel).count()
* callback is a function taking (dsl_value, done) where done is to be called
* in the fashion (error, result) at the end
* returns a future whose value is determined by done(error, result)
* ex.
* var linkFuture = doWith(element("a.myLink").attr("href"), function(href, done) {
* if (!href) {
* done("bad attr!")
* return;
* }
*
* console.log("got link", href);
* done(null, href);
* });
*
* // this assertion always passes
* expect(doWith(element(sel).count(), function(count, done) {
* console.log("got", count);
* done(null, 1000);
* })).toBe(1000);
*/
angular.scenario.dsl('doWithM', function() {
return function(future, fn, desc) {
var self = this;
return this.addFuture(desc || '(doing something)', function(done) {
fn.call(self, future.value, done);
});
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment