admin管理员组文章数量:1296485
I'm getting more and more confortable with the asyncawait library.
If I'm correct, this:
var doSth = async function() {
var asyncResult = await promiseReturningFunction();
subFunction(asyncResult);
};
is equivalent to:
var doSth = function() {
promiseReturningFunction().then(function(asyncResult) {
subFunction(asyncResult);
});
};
But what if the callback has two arguments .then(function(asyncResult1, asyncResult2) {})
?
In some other languages, I'd write:
var asyncResult1, asyncResult2 = await promiseReturningFunction();
but I can't do this in JS, can I? Does await returns an array? The first argument?
I'm getting more and more confortable with the asyncawait library.
If I'm correct, this:
var doSth = async function() {
var asyncResult = await promiseReturningFunction();
subFunction(asyncResult);
};
is equivalent to:
var doSth = function() {
promiseReturningFunction().then(function(asyncResult) {
subFunction(asyncResult);
});
};
But what if the callback has two arguments .then(function(asyncResult1, asyncResult2) {})
?
In some other languages, I'd write:
var asyncResult1, asyncResult2 = await promiseReturningFunction();
but I can't do this in JS, can I? Does await returns an array? The first argument?
Share Improve this question asked Feb 25, 2016 at 17:30 Augustin RiedingerAugustin Riedinger 22.3k32 gold badges145 silver badges219 bronze badges 2-
1
The asyncawait library and the uping
async/await
are two different things. They may work in a very similar way, but you should clarify which one you are talking about. Even though you are linking to the library, your example doesn't seem to use the library. – Felix Kling Commented Feb 25, 2016 at 17:49 - Are you using the experimental async/await transpiler, or that fiber-based library? – Bergi Commented Feb 25, 2016 at 17:54
3 Answers
Reset to default 8I can't do this in JS, can I?
No, you can't. Promises fulfill with only a single value.
JavaScript doesn't have tuples, but you can use an array for that. With destructuring, you can make it look almost like in those other languages:
async function promiseReturningFunction() {
await …;
…
return [result1, result2];
}
// elsewhere (in an async function):
var [asyncResult1, asyncResult2] = await promiseReturningFunction();
You can easily use the destructuring
on a bunch of promises like that:
var [r1, r2, r3] = await Promise.all([p1, p2, p3]);
or
var promises = [p1(), p2(), p3()];
$q.all(promises)
.then(function ([p1Result, p2Result, p3Result]) {
// ...
});
So you will have:
async function doSth() {
var [asyncResult1, asyncResult1] = await promiseReturningFunction();
var asyncLastResult = await subFunction(asyncResult1);
return asyncLastResult;
}
doSth();
Promises can only pass one argument to the fulfillment callback. As Benjamin Gruenbaum writes:
[...] Just the first parameter will be treated as resolution value in the promise constructor. You can resolve with a posite value like an object or array.
(Here is the section in the Promises/A+ specification)
So your case with two arguments doesn't occur in JavaScript (at least not with standard promise implementations). Just use an array or an object (depending on your data structure).
本文标签: javascriptAsyncawaitjs multiple arguments in callbackStack Overflow
版权声明:本文标题:javascript - Asyncawait.js multiple arguments in callback - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741630036a2389307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论