面试技术问询:$q.all方法的正确使用语法有哪些?
$q.all() Method Let's break down each option to figure out which are valid uses of $q.all():
First option (array of promises):
$q.all([promise1(), promise2()]).then((values) => { … });This is valid.
$q.all()accepts an array of promise instances (or calls to promise-returning functions, since invoking them returns a promise). The.then()callback will receive an array of resolved values, ordered to match the input promises.Second option (individual arguments):
$q.all("promise1", "promise2").then((values) => { … });This is invalid.
$q.all()doesn't accept multiple standalone arguments—it expects either a single array or a single object as its only parameter. On top of that, passing string literals here is meaningless, since they aren't promises.Third option (object of promises):
$q.all({"promise1": promise1(), "promise2": promise2()}).then((values) => { ... });This is valid.
$q.all()also supports an object where each property maps to a promise (or a call to a promise-returning function). The.then()callback will receive an object with the same keys, each mapped to their corresponding resolved value.
To sum up: the first and third options are the correct syntax for $q.all().
内容的提问来源于stack exchange,提问作者Rorbert Gilbert

