q - Implementing promise queues with native promises? -
q - Implementing promise queues with native promises? -
i refer first-class article "a general theory of reactivity" @kriskowal. in it, gives next implementation of promises queue, using q conventions:
function promisequeue() { var ends = promise.defer(); this.put = function (value) { var next = promise.defer(); ends.resolve({ head: value, tail: next.promise }); ends.resolve = next.resolve; }; this.get = function () { var result = ends.promise.get("head"); ends.promise = ends.promise.get("tail"); homecoming result; }; }
you'll see it's using q features such defer
, get
. i'm trying wrap ahead around how adapt utilize native promises, experiencing massive brain freeze. (in process of working on this, came across fact chrome appears provide promise.defer
method, afaik not in spec.)
can help?
oh wow that's cool people reading gtor :d
in es6 promises, replacement .defer
promise constructor:
this.put = function (value) { var r; var p = new promise(function(resolve){ r = resolve; }); ends.resolve({ head: value, tail: p }); ends.resolve = r; };
and .get('foo')
syntactic sugar .then(function(val){ homecoming val['foo']; })
translates rather directly:
this.get = function () { var result = ends.promise.then(function(v){ homecoming v["head"]; }); ends.promise = ends.promise.then(function(v){ homecoming v["tail"]; }); homecoming result; };
similarly, since don't have ends
map promise constructor - can do:
var o = {}; var p = new promise(function(res, rej){ o.res = res; o.rej = rej; }); o.promise = p; // o deferred
note kris's queue illustrative purposes , explain concept - never got utilize practical. utilize case scenario subtle, if want queue promises you'd utilize .then
.
promise q es6-promise
Comments
Post a Comment