Both functions return Promise
.
const [result1, result2] = await Promise.all([promise1, promise2])
Where I wrote HERE, you can access result1 and result2 var, which are the results of promises.
Waiting is an alternative to calling then
on Promise
, and its form is also more readable than
Promise.all([promise1, promise2]).then(function(results){ });
If you have multiple consecutive requests using await, this is the best choice.
var response1= await promise1 var response2=await promise2
vs
promise1.then(function(){ promise2.then(function(){ promise3.then(function(){ }) }) })
EDIT
In the first function, the async keyword is useless, the test function will return Promise
The second function will return a promise in which you will see the await keyword. When the expected promise is resolved, execution within the function continues, and you can access the result of the promise
EDIT 1
Perhaps I understand what you mean, the async keyword encapsulates the return value in a promise that has as the permitted value that you returned.
source share