I was asked to clarify what I meant by my two examples so here are some snippets of code that illustrate it. This is forcibly async but you get the point I hope. If you want to add three numbers you can define:
function add3(foo, bar, baz) { return Task.spawn(() => { let sum = yield add2(foo, bar); return yield add2(sum, baz); }); } Then you need add2 which you can define as either: function add2(a, b) { let sum = yield a + b; return sum; } or as: function add2(a, b) { return Task.spawn(() => { let sum = yield a + b; return sum; }); } The latter is certainly a little more verbose with two additional lines of code and some extra indentation but it is also usable standalone whereas the first example isn't, you need to know how to call it as a generator. On Tue, Oct 8, 2013 at 11:47 AM, Dave Townsend <dtowns...@mozilla.com>wrote: > As Task.jsm is used more throughout our code it would be good to try to > use similar sorts of patterns to avoid confusion. > > One difference I've spotted is in how to write asynchronous functions that > are called by tasks. One way is to simply write the function as a > generator, the other is to write the function as something that returns a > promise by wrapping its code with another Task.spawn call. In both cases > the calling task just yields the result of the function to wait for it to > complete. > > I'm going to claim that the latter method of returning a new task promise > is the one we should use in general. It makes the function more easily > usable outside of a task since you're just getting a promise back. It is > also what Task.jsm does internally for generators anyway. > > Does anyone disagree? > _______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform