How To Use Multiple Xmlhttprequest.responsetext Values?
In javascript, how can I best combine multiple values which are obtained as arguments to callback functions only, preferably without adding library dependencies? E.g. consider func
Solution 1:
Apparently the async/wait keywords are what I was looking for. With these my mockup example can be written as
asyncfunctionasync_await_2(){
var jobs = newPromise(function(resolve,reject){
var req = newXMLHttpRequest();
req.open('GET', '/jobs');
req.onload = function() { resolve(req.responseText); };
req.send();
});
var some = newPromise(function(resolve,reject){
var handle = someAsyncAPI();
handle.callback = resolve;
handle.send();
});
var other = newPromise(function(resolve,reject){
otherAsyncAPI(resolve);
});
console.log({
mode: 'async-await',
jobs: await jobs,
some: (await some).value,
other: (await other).value});
}
or alternatively (might cause sequential instead of parallel execution of the promises)
asyncfunctionasync_await(){
// Concise, but sacrifices parallelism of the Promises?var jobs = awaitnewPromise(function(resolve,reject){
var req = newXMLHttpRequest();
req.open('GET', '/jobs');
req.onload = function() { resolve(req.responseText); };
req.send();
});
var some = awaitnewPromise(function(resolve,reject){
var handle = someAsyncAPI();
handle.callback = resolve;
handle.send();
});
var other = awaitnewPromise(function(resolve,reject){
otherAsyncAPI(resolve);
});
console.log({
mode: 'async-await',
jobs: jobs,
some: some.value,
other: other.value});
}
Post a Comment for "How To Use Multiple Xmlhttprequest.responsetext Values?"