javascript - Error using spread with Q.all -
javascript - Error using spread with Q.all -
i utilize q.all
in conjunction spread
in order migrate 2 promise.then
beingness simultaneously performed on successful resolution of promise
:
var p1=112; var p2=function(data){ console.log(data); homecoming getformdatawithdropdown(objtype,scenario); }; var guidrequest=processguidrequest(); if(!q.ispromise(guidrequest)) { guidrequest=q(guidrequest); } guidrequest.all([p1,p2]) .spread(function(geoval,formval){ console.log(geoval); console.log(formval); }).done();
p1
value
, p2
function returns function
called getformdatawithdropdown returns promise
or value
based on resolution of chained set of promise
s.however error when run code:
uncaught typeerror: function.prototype.apply: arguments list has wrong type
the error occurs in function:
promise.prototype.spread = function (fulfilled, rejected) { homecoming this.all().then(function (array) { homecoming fulfilled.apply(void 0, array);//error occurs here }, rejected);
};
there serious mistakes in code (and bit fault in q, .all
apparently accomplishes homecoming non-arrays).
all
not take callback. sole argument either array, or promise array. can called either static method:
q.all([…])
or method on promise (the promise array):
q([…]) .all()
now, array (wherever comes from) needs contain promises - promises awaited then. passing number , function. should wrap plain value in promise q(p1)
(even if not strict necessary), , need phone call function gives promise (and if it's function function say, need phone call twice).
if want wait guidrequest
, need callback function - if want or not.
notice q.ispromise
on guidrequest
unnecessary, seek cast q
.
var v1=112; function f2(data){ console.log(data); homecoming getformdatawithdropdown(objtype,scenario); }; var guidrequest = q(processguidrequest()); guidrequest.then(function(guid) { var p1 = q(v1), p2 = f2(guid); // promises here! homecoming q.all([p1, p2]); }).spread(function(geoval,formval){ console.log(geoval); console.log(formval); }).done();
alternatively, have written return [p1, p2]
, automatically have been awaited spread
.
javascript browser promise q
Comments
Post a Comment